2020-03-19 04:52:14 +00:00
|
|
|
#include "qwaq-button.h"
|
|
|
|
#include "qwaq-listener.h"
|
|
|
|
|
|
|
|
@implementation Button
|
|
|
|
-initWithPos: (Point) pos releasedIcon: (DrawBuffer *) released
|
|
|
|
pressedIcon: (DrawBuffer *) pressed
|
|
|
|
{
|
|
|
|
Extent size = mergeExtents ([released size], [pressed size]);
|
|
|
|
|
|
|
|
if (!(self = [super initWithRect: {pos, size}])) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
icon[0] = released;
|
|
|
|
icon[1] = pressed;
|
|
|
|
onPress = [[ListenerGroup alloc] init];
|
|
|
|
onRelease = [[ListenerGroup alloc] init];
|
|
|
|
onClick = [[ListenerGroup alloc] init];
|
|
|
|
onDrag = [[ListenerGroup alloc] init];
|
|
|
|
onAuto = [[ListenerGroup alloc] init];
|
2020-03-19 04:57:10 +00:00
|
|
|
onHover = [[ListenerGroup alloc] init];
|
2020-03-19 04:52:14 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-23 13:00:05 +00:00
|
|
|
-initWithRect: (Rect) rect
|
|
|
|
{
|
|
|
|
if (!(self = [super initWithRect: rect])) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
icon[0] = nil;
|
|
|
|
icon[1] = nil;
|
|
|
|
onPress = [[ListenerGroup alloc] init];
|
|
|
|
onRelease = [[ListenerGroup alloc] init];
|
|
|
|
onClick = [[ListenerGroup alloc] init];
|
|
|
|
onDrag = [[ListenerGroup alloc] init];
|
|
|
|
onAuto = [[ListenerGroup alloc] init];
|
|
|
|
onHover = [[ListenerGroup alloc] init];
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-19 04:52:14 +00:00
|
|
|
-draw
|
|
|
|
{
|
2020-03-19 09:40:21 +00:00
|
|
|
[super draw];
|
2020-03-23 13:00:05 +00:00
|
|
|
if (icon[pressed]) {
|
|
|
|
[textContext blitFromBuffer: icon[pressed]
|
|
|
|
to: pos
|
|
|
|
from: [icon[pressed] rect]];
|
|
|
|
}
|
2020-03-19 04:52:14 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-handleEvent: (qwaq_event_t *) event
|
|
|
|
{
|
|
|
|
ListenerGroup *action = nil;
|
|
|
|
|
2020-03-26 02:04:57 +00:00
|
|
|
[super handleEvent: event];
|
|
|
|
|
2020-03-19 04:52:14 +00:00
|
|
|
if (event.what & qe_mouse) {
|
|
|
|
switch ((qwaq_mouse_event) (event.what & qe_mouse)) {
|
|
|
|
case qe_mousedown:
|
|
|
|
pressed = 1;
|
2020-03-19 10:25:57 +00:00
|
|
|
click = 0;
|
|
|
|
dragBase = {event.mouse.x, event.mouse.y};
|
2020-03-19 04:52:14 +00:00
|
|
|
action = onPress;
|
2020-03-19 10:00:03 +00:00
|
|
|
[self grabMouse];
|
|
|
|
[self redraw];
|
2020-03-19 04:52:14 +00:00
|
|
|
break;
|
|
|
|
case qe_mouseup:
|
|
|
|
pressed = 0;
|
2020-03-19 10:25:57 +00:00
|
|
|
click = 0;
|
2020-03-19 04:52:14 +00:00
|
|
|
action = onRelease;
|
2020-03-19 10:00:03 +00:00
|
|
|
[self releaseMouse];
|
2020-03-19 10:25:57 +00:00
|
|
|
if ([self containsPoint: {event.mouse.x, event.mouse.y}]) {
|
|
|
|
[onClick respond: self];
|
|
|
|
}
|
2020-03-19 10:00:03 +00:00
|
|
|
[self redraw];
|
2020-03-19 04:52:14 +00:00
|
|
|
break;
|
|
|
|
case qe_mouseclick:
|
|
|
|
action = onClick;
|
2020-03-19 10:25:57 +00:00
|
|
|
click = event.mouse.click;
|
2020-03-19 04:52:14 +00:00
|
|
|
break;
|
|
|
|
case qe_mousemove:
|
2020-03-19 10:25:57 +00:00
|
|
|
click = 0;
|
2020-03-19 04:52:14 +00:00
|
|
|
if (pressed) {
|
2020-03-19 10:25:57 +00:00
|
|
|
dragPos = {event.mouse.x, event.mouse.y};
|
2020-03-19 04:52:14 +00:00
|
|
|
action = onDrag;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qe_mouseauto:
|
2020-03-22 05:06:27 +00:00
|
|
|
if (pressed
|
|
|
|
&& [self containsPoint: {event.mouse.x, event.mouse.y}]) {
|
|
|
|
click = 0;
|
|
|
|
action = onAuto;
|
|
|
|
}
|
2020-03-19 04:52:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (action) {
|
|
|
|
[action respond: self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(ListenerGroup *) onPress
|
|
|
|
{
|
|
|
|
return onPress;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(ListenerGroup *) onRelease
|
|
|
|
{
|
|
|
|
return onRelease;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(ListenerGroup *) onClick
|
|
|
|
{
|
|
|
|
return onClick;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(ListenerGroup *) onDrag
|
|
|
|
{
|
|
|
|
return onDrag;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(ListenerGroup *) onAuto
|
|
|
|
{
|
|
|
|
return onAuto;
|
|
|
|
}
|
|
|
|
|
2020-03-19 04:57:10 +00:00
|
|
|
-(ListenerGroup *) onHover
|
|
|
|
{
|
|
|
|
return onHover;
|
|
|
|
}
|
2020-03-19 10:25:57 +00:00
|
|
|
|
|
|
|
- (int) click
|
|
|
|
{
|
|
|
|
return click;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Point) delta
|
|
|
|
{
|
|
|
|
return {dragPos.x - dragBase.x, dragPos.y - dragBase.y};
|
|
|
|
}
|
|
|
|
|
2020-03-23 13:10:03 +00:00
|
|
|
-move:(Point) delta
|
|
|
|
{
|
|
|
|
Point pos = self.pos;
|
|
|
|
[super move:delta];
|
|
|
|
dragBase.x += self.pos.x - pos.x;
|
|
|
|
dragBase.y += self.pos.y - pos.y;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-19 04:52:14 +00:00
|
|
|
@end
|