2020-06-21 14:15:17 +00:00
|
|
|
#include "ruamoko/qwaq/ui/button.h"
|
|
|
|
#include "ruamoko/qwaq/ui/listener.h"
|
2020-03-19 04:52:14 +00:00
|
|
|
|
|
|
|
@implementation Button
|
2020-03-30 07:30:58 +00:00
|
|
|
|
|
|
|
+(Button *)withPos:(Point)pos releasedIcon:(DrawBuffer *)released
|
|
|
|
pressedIcon:(DrawBuffer *)pressed
|
|
|
|
{
|
|
|
|
return [[[self alloc] initWithPos:pos
|
|
|
|
releasedIcon:released
|
|
|
|
pressedIcon:pressed] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
+(Button *)withRect:(Rect)rect
|
|
|
|
{
|
|
|
|
return [[[self alloc] initWithRect:rect] autorelease];
|
|
|
|
}
|
|
|
|
|
2020-03-19 04:52:14 +00:00
|
|
|
-initWithPos: (Point) pos releasedIcon: (DrawBuffer *) released
|
|
|
|
pressedIcon: (DrawBuffer *) pressed
|
|
|
|
{
|
|
|
|
Extent size = mergeExtents ([released size], [pressed size]);
|
|
|
|
|
|
|
|
if (!(self = [super initWithRect: {pos, size}])) {
|
|
|
|
return nil;
|
|
|
|
}
|
2020-03-30 07:30:58 +00:00
|
|
|
icon[0] = [released retain];
|
|
|
|
icon[1] = [pressed retain];
|
2020-03-31 04:57:22 +00:00
|
|
|
onPress = [[ListenerGroup listener] retain];
|
|
|
|
onRelease = [[ListenerGroup listener] retain];
|
|
|
|
onClick = [[ListenerGroup listener] retain];
|
|
|
|
onDrag = [[ListenerGroup listener] retain];
|
|
|
|
onAuto = [[ListenerGroup listener] retain];
|
|
|
|
onHover = [[ListenerGroup listener] retain];
|
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;
|
2020-03-31 04:57:22 +00:00
|
|
|
onPress = [[ListenerGroup listener] retain];
|
|
|
|
onRelease = [[ListenerGroup listener] retain];
|
|
|
|
onClick = [[ListenerGroup listener] retain];
|
|
|
|
onDrag = [[ListenerGroup listener] retain];
|
|
|
|
onAuto = [[ListenerGroup listener] retain];
|
|
|
|
onHover = [[ListenerGroup listener] retain];
|
2020-03-23 13:00:05 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2020-03-30 07:30:58 +00:00
|
|
|
-(void)dealloc
|
|
|
|
{
|
|
|
|
[icon[0] release];
|
|
|
|
[icon[1] release];
|
2020-03-31 04:57:22 +00:00
|
|
|
[onPress release];
|
|
|
|
[onRelease release];
|
|
|
|
[onClick release];
|
|
|
|
[onDrag release];
|
|
|
|
[onAuto release];
|
|
|
|
[onHover release];
|
2020-03-30 07:30:58 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
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];
|
|
|
|
}
|
2020-03-30 14:42:06 +00:00
|
|
|
event.what = qe_none;
|
2020-03-19 04:52:14 +00:00
|
|
|
}
|
|
|
|
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
|