Skip to content

Instantly share code, notes, and snippets.

@RblSb

RblSb/Button.hx Secret

Created November 7, 2018 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RblSb/ea9b13a24bfdb9ae0bbd7a0826d498de to your computer and use it in GitHub Desktop.
Save RblSb/ea9b13a24bfdb9ae0bbd7a0826d498de to your computer and use it in GitHub Desktop.
meh button
package game.gui;
import haxe.Constraints.Function;
import kha.graphics2.Graphics;
import kha.math.FastMatrix3;
import kha.FastFloat;
import kha.Image;
import kha.input.KeyCode;
import khm.Types.Rect;
import khm.Screen;
import khm.Screen.Pointer;
@:structInit
class ButtonSets {
public var x:Float;
public var y:Float;
public var w = -1.0;
public var h = -1.0;
public var clickMode = false;
@:optional public var image:Image;
@:optional public var imageId:String;
@:optional public var imageRect:Rect;
public var angle = 0.0;
public var keys:Array<KeyCode> = [];
@:optional public var onDown:Function;
}
class Button {
public var rect:Rect;
var keys:Array<KeyCode>;
var isDown = false;
var onDownFunc:Function;
var clickMode:Bool;
var img:Image;
var imgId:String;
var imgRect:Rect;
var angle:Float;
var pointerId = -1;
public function new(sets:ButtonSets) {
rect = {x: sets.x, y: sets.y, w: sets.w, h: sets.h};
clickMode = sets.clickMode;
imgId = sets.imageId;
if (imgId != null) img = kha.Assets.images.get(imgId);
else img = sets.image;
imgRect = sets.imageRect;
if (img != null) {
if (rect.w == -1 || rect.h == -1) {
rect.w = img.width;
rect.h = img.height;
}
if (imgRect == null) {
imgRect = {x: 0, y: 0, w: img.width, h: img.height};
}
}
angle = sets.angle;
keys = sets.keys;
onDownFunc = sets.onDown;
}
var transformation = FastMatrix3.identity();
public function render(g:Graphics):Void {
if (isDown) {
g.color = 0x50FFFFFF;
g.fillRect(rect.x, rect.y, rect.w, rect.h);
}
g.color = 0xFFFFFFFF;
if (angle != 0) {
transformation.setFrom(g.transformation);
g.transformation = g.transformation.multmat(
rotation(angle * Math.PI / 180, rect.x + rect.w / 2, rect.y + rect.h / 2)
);
}
if (imgId != null) Atlas.drawSubImage(
g, imgId, rect.x, rect.y,
imgRect.x, imgRect.y, imgRect.w, imgRect.h
);
else if (img != null) g.drawSubImage(
img, rect.x, rect.y,
imgRect.x, imgRect.y, imgRect.w, imgRect.h
);
if (angle != 0) {
g.transformation = transformation;
}
}
inline function rotation(angle:FastFloat, centerX:FastFloat, centerY:FastFloat): FastMatrix3 {
return FastMatrix3.translation(centerX, centerY)
.multmat(FastMatrix3.rotation(angle))
.multmat(FastMatrix3.translation(-centerX, -centerY));
}
public static function onDown(screen:Screen, buttons:Array<Button>, p:Pointer):Bool {
var result = false;
// down pressed button
for (b in buttons)
if (b.isInside(p.x, p.y)) {
for (i in b.keys) {
screen.onKeyDown(i);
screen.keys[i] = true;
}
if (b.onDownFunc != null) b.onDownFunc(p);
b.isDown = true;
b.pointerId = p.id;
result = true;
/*if (b.clickMode) {
onUp(screen, buttons, p);
}*/
}
return result;
}
public static function onMove(screen:Screen, buttons:Array<Button>, p:Pointer):Bool {
if (!p.isDown) return false;
if (!isActive(buttons, p)) return false;
// up other buttons
for (b in buttons) {
if (b.pointerId != p.id) continue;
if (b.isDown && !b.isInside(p.x, p.y)) {
for (i in b.keys) screen.keys[i] = false;
b.isDown = false;
}
}
// down current button
for (b in buttons)
if (b.isInside(p.x, p.y)) {
if (!b.isDown) { // !b.clickMode ||
for (i in b.keys) screen.keys[i] = true;
b.isDown = true;
b.pointerId = p.id;
}
}
return true;
}
public static function onUp(screen:Screen, buttons:Array<Button>, p:Pointer):Bool {
if (!isActive(buttons, p)) return false;
// up latest pressed button
for (b in buttons)
if (b.isInside(p.x, p.y)) {
for (i in b.keys) {
screen.onKeyUp(i);
screen.keys[i] = false;
}
b.isDown = false;
}
return true;
}
inline function isInside(x:Int, y:Int):Bool {
if (x < rect.x || x >= rect.x + rect.w || y < rect.y || y >= rect.y + rect.h) return false;
return true;
}
static inline function isActive(buttons:Array<Button>, p:Pointer):Bool {
var active = false; // if you pressed buttons
for (b in buttons)
if (b.isInside(p.startX, p.startY)) {
active = true;
break;
}
return active;
}
}
package game.gui;
import kha.graphics2.Graphics;
import kha.Canvas;
import kha.Assets;
import kha.input.KeyCode;
import khm.Screen;
class SoundScreen extends Screen {
var buttons:Array<Button>;
public function init():Void {
final on = Assets.images.gui_sound_on;
final off = Assets.images.gui_sound_off;
final y = Screen.h / 2 - on.height / 2;
final x = Screen.w / 4 - off.width / 2;
final x2 = Screen.w / 2 + Screen.w / 4 - on.width / 2;
buttons = [
new Button({x: x, y: y, imageId: "gui_sound_off", keys: [Q]}),
new Button({x: x2, y: y, imageId: "gui_sound_on", keys: [E]})
];
}
function newGame():Void {
var game = new game.Game();
game.show();
game.init();
}
override function onResize():Void {
init();
}
override function onMouseDown(p:Pointer):Void {
if (Button.onDown(this, buttons, p)) return;
}
override function onMouseUp(p:Pointer):Void {
if (Button.onUp(this, buttons, p)) return;
}
override function onMouseMove(p:Pointer):Void {
if (Button.onMove(this, buttons, p)) return;
}
override function onKeyDown(key:KeyCode):Void {
if (key == Q) {
Music.isEnabled = false;
newGame();
} else if (key == E || key == Return || key == Space) {
Music.isEnabled = true;
newGame();
}
}
override function onRender(canvas:Canvas):Void {
final g = canvas.g2;
g.begin(0xFF2D0B09);
drawBG(g);
for (button in buttons) button.render(g);
g.end();
}
function drawBG(g:Graphics):Void {
g.color = 0xFFFFFFFF;
final head = Assets.images.gui_menu_back_1;
var offY = head.height;
g.drawScaledImage(head, 0, 0, Screen.w, head.height);
final bottom = Assets.images.gui_menu_back_2;
var bottomY = Screen.h - bottom.height;
if (bottomY < head.height) bottomY = head.height;
g.drawScaledImage(bottom, 0, bottomY, Screen.w, bottom.height);
final logo = Assets.images.gui_menu_pict;
g.drawImage(logo, Screen.w / 2 - logo.width / 2, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment