Debian Codegraph @

stylus.cpp

raw symbols SCIP
1 /*
2 * PROPRIETARY INFORMATION. This software is proprietary to POWDER
3 * Development, and is not to be reproduced, transmitted, or disclosed
4 * in any way without written permission.
5 *
6 * Produced by: Jeff Lait
7 *
8 * POWDER Development
9 *
10 * NAME: stylus.cpp ( POWDER Library, C++ )
11 *
12 * COMMENTS:
13 */
14
15 #include "stylus.h"
16 #include <mygba.h>
17 #include "gfxengine.h"
18 #include "assert.h"
19
20 #ifdef USING_SDL
21 #define TILEWIDTH (gfx_gettilewidth())
22 #define TILEHEIGHT (gfx_gettileheight())
23 #define MODBYTILE(x, tw) (x - ((x)%(tw)))
24 #define SNAPTX(x) (MODBYTILE(x,TILEWIDTH*2) + TILEWIDTH)
25 #define SNAPTY(y) (MODBYTILE(y,TILEHEIGHT*2) + TILEHEIGHT)
26 #define SNAPCX(x) (MODBYTILE(x,TILEWIDTH) + TILEWIDTH/2)
27 #define SNAPCY(y) (MODBYTILE(y,TILEHEIGHT) + TILEHEIGHT/2)
28 #define SNAPTX_o(x) (MODBYTILE(x+TILEWIDTH,TILEWIDTH*2))
29 #define SNAPTY_o(y) (MODBYTILE(y+TILEHEIGHT,TILEHEIGHT*2))
30
31 #define SNAPTX_k(x) (SNAPTX(x-myLeft+TILEWIDTH/2)+myLeft-TILEWIDTH/2)
32 #define SNAPTY_k(y) (SNAPTY(y-myTop+TILEHEIGHT/2)+myTop-TILEHEIGHT/2)
33 #else
34 #define TILEWIDTH 8
35 #define TILEHEIGHT 8
36 #define SNAPTX(x) (((x) & ~15)+8)
37 #define SNAPTY(x) SNAPTX(x)
38 #define SNAPCX(x) (((x) & ~7)+4)
39 #define SNAPCY(x) SNAPCX(x)
40 #define SNAPTX_o(x) ((x+8) & ~15)
41 #define SNAPTY_o(x) SNAPTX_o(x)
42
43 #define SNAPTX_k(x) (((x-myLeft+4) & ~15)+4+myLeft)
44 #define SNAPTY_k(x) (((x-myTop+4) & ~15)+4+myTop)
45 #endif
46
47 // Ugliness for our dragging of inventory items.
48 #include "creature.h"
49 #include "item.h"
50
51 STYLUSLOCK *glbStylusActive = 0;
52
53 STYLUSLOCK::STYLUSLOCK(int regions)
54 {
55 myParent = glbStylusActive;
56 myDragging = false;
57 if (regions)
58 setRegion(regions);
59 }
60
61 void
62 STYLUSLOCK::setRegion(int regions)
63 {
64 myParent = glbStylusActive;
65 glbStylusActive = this;
66 myDragging = false;
67
68 // Ensure we start with a clean slate.
69 clear();
70
71 // If the stylus is currently down we don't want to process it
72 // since it is a press from before our stylus lock.
73 myWaitForDown = hamfake_getstylusstate();
74
75 myRegion = regions;
76
77 myLeft = myRight = myTop = myBottom = -1;
78 }
79
80 STYLUSLOCK::~STYLUSLOCK()
81 {
82 glbStylusActive = myParent;
83
84 // Even if we left by another code path we don't want
85 // to leave half a click.
86 clear();
87 }
88
89 bool
90 STYLUSLOCK::inRange(int x, int y)
91 {
92 if (myLeft >= 0 && x < myLeft)
93 return false;
94
95 if (myRight >= 0 && x > myRight)
96 return false;
97
98 if (myTop >= 0 && y < myTop)
99 return false;
100
101 if (myBottom >= 0 && y > myBottom)
102 return false;
103
104 return true;
105 }
106
107 bool
108 STYLUSLOCK::snap(int &x, int &y)
109 {
110 bool valid = false;
111 if (myRegion & REGION_BOTTOMBUTTON)
112 {
113 if ((y >= 20*TILEHEIGHT || y < 0) &&
114 x >= 0 && x < 30*TILEWIDTH)
115 {
116 valid = true;
117 if (y >= 20*TILEHEIGHT)
118 y = 20*TILEHEIGHT + TILEHEIGHT;
119 else
120 y = -TILEHEIGHT;
121 x = SNAPTX(x);
122 gfx_cursortile(SPRITE_TILE);
123 }
124 }
125 if (myRegion & REGION_SIDEBUTTON)
126 {
127 if (y < 20*TILEHEIGHT && y >= 0)
128 {
129 if (x < TILEWIDTH || x >= 29*TILEWIDTH)
130 {
131 valid = true;
132 if (x > 20*TILEWIDTH)
133 x = 30*TILEWIDTH;
134 else
135 x = 0;
136 // Set the y pos
137 y = SNAPTY(y);
138 gfx_cursortile(SPRITE_TILE);
139 }
140 }
141 }
142 if (myRegion & REGION_MAPTILES)
143 {
144 x = SNAPTX(x);
145 y = SNAPTY_o(y);
146 valid = true;
147 gfx_cursortile(SPRITE_CURSOR);
148 }
149 if (myRegion & REGION_KEYBOARD)
150 {
151 if (inRange(x, y))
152 {
153 x = SNAPTX_k(x);
154 y = SNAPTY_k(y);
155 valid = true;
156 gfx_cursortile(SPRITE_TEXT);
157 }
158 else
159 {
160 // Rest of map is used for cancelling.
161 valid = true;
162 gfx_cursortile(SPRITE_CANCEL);
163 }
164 }
165 if (myRegion & REGION_INVENTORY)
166 {
167 int sx = x, sy = y;
168
169 valid = true;
170 if (sx >= TILEWIDTH*4 && sy >= TILEHEIGHT*2 && sx < TILEWIDTH*30 && sy < TILEHEIGHT*18)
171 {
172 // Ignore central column
173 if (sx < TILEWIDTH*6 || sx >= TILEWIDTH*8)
174 {
175 gfx_cursortile(SPRITE_CURSOR);
176 x = SNAPTX(x);
177 y = SNAPTY(y);
178 }
179 else
180 gfx_cursortile(SPRITE_CANCEL);
181 }
182 else
183 gfx_cursortile(SPRITE_CANCEL);
184 }
185 if (myRegion & REGION_SLOTS)
186 {
187 int sx = x, sy = y;
188
189 valid = true;
190 if (sx >= TILEWIDTH*4 && sy >= TILEHEIGHT*2 && sx < TILEWIDTH*6 && sy < TILEHEIGHT*18)
191 {
192 gfx_cursortile(SPRITE_CURSOR);
193 x = SNAPTX(x);
194 y = SNAPTX(y);
195 }
196 else
197 {
198 gfx_cursortile(SPRITE_CANCEL);
199 }
200 }
201 if (myRegion & REGION_MENU)
202 {
203 if (inRange(x, y))
204 {
205 valid = true;
206 gfx_cursortile(SPRITE_TEXT);
207 x = SNAPCX(x);
208 y = SNAPCY(y);
209 }
210 else
211 {
212 // Rest of map is used for cancelling.
213 valid = true;
214 gfx_cursortile(SPRITE_CANCEL);
215 }
216 }
217
218 if (myRegion & REGION_DISPLAYTEXT)
219 {
220 valid = true;
221 if (y < TILEHEIGHT*8)
222 gfx_cursortile(SPRITE_NORTH);
223 else if (y < TILEHEIGHT*15)
224 gfx_cursortile(SPRITE_CANCEL);
225 else
226 gfx_cursortile(SPRITE_SOUTH);
227 }
228
229 if (myRegion & REGION_TENDIR)
230 {
231 int sx = x, sy = y;
232
233 // Select around center
234 sy += TILEHEIGHT;
235 sx /= TILEWIDTH*2;
236 sy /= TILEHEIGHT*2;
237 sx -= 7;
238 sy -= 5;
239 // We now have +/-1 with option of +/-2 in y.
240 if ((ABS(sx) <= 1 && ABS(sy) <= 1) ||
241 (sx == 0 && ABS(sy) == 2))
242 {
243 valid = true;
244 x = SNAPTX(x);
245 y = SNAPTY_o(y);
246 switch (sx)
247 {
248 case -1:
249 if (sy == -1)
250 gfx_cursortile(SPRITE_NORTHWEST);
251 else if (sy == 0)
252 gfx_cursortile(SPRITE_WEST);
253 else
254 gfx_cursortile(SPRITE_SOUTHWEST);
255 break;
256 case 0:
257 if (sy == -2)
258 gfx_cursortile(SPRITE_UP);
259 else if (sy == 2)
260 gfx_cursortile(SPRITE_DOWN);
261 else if (sy == -1)
262 gfx_cursortile(SPRITE_NORTH);
263 else if (sy == 0)
264 gfx_cursortile(SPRITE_CURSOR);
265 else
266 gfx_cursortile(SPRITE_SOUTH);
267 break;
268 case 1:
269 if (sy == -1)
270 gfx_cursortile(SPRITE_NORTHEAST);
271 else if (sy == 0)
272 gfx_cursortile(SPRITE_EAST);
273 else
274 gfx_cursortile(SPRITE_SOUTHEAST);
275 break;
276 }
277 }
278 else
279 {
280 // Cancel
281 valid = true;
282 gfx_cursortile(SPRITE_CANCEL);
283 }
284 }
285
286 if (myRegion & REGION_FOURDIR)
287 {
288 int sx = x, sy = y;
289
290 // Select around center
291 sy += TILEHEIGHT;
292 sx /= TILEWIDTH*2;
293 sy /= TILEHEIGHT*2;
294 sx -= 7;
295 sy -= 5;
296 if (ABS(sx) <= 1 && ABS(sy) <= 1 && (sx * sy == 0))
297 {
298 valid = true;
299 x = SNAPTX(x);
300 y = SNAPTY_o(y);
301 switch (sx)
302 {
303 case -1:
304 gfx_cursortile(SPRITE_WEST);
305 break;
306 case 0:
307 if (sy == -1)
308 gfx_cursortile(SPRITE_NORTH);
309 else if (sy == 0)
310 gfx_cursortile(SPRITE_CURSOR);
311 else
312 gfx_cursortile(SPRITE_SOUTH);
313 break;
314 case 1:
315 gfx_cursortile(SPRITE_EAST);
316 break;
317 }
318 }
319 else
320 {
321 // Cancel
322 valid = true;
323 gfx_cursortile(SPRITE_CANCEL);
324 }
325 }
326 return valid;
327 }
328
329 void
330 STYLUSLOCK::setRange(int left, int top, int right, int bottom)
331 {
332 myLeft = left*TILEWIDTH;
333 myTop = top*TILEHEIGHT;
334 myRight = right*TILEWIDTH + TILEWIDTH-1;
335 myBottom = bottom*TILEHEIGHT + TILEHEIGHT-1;
336 }
337
338 void
339 STYLUSLOCK::update()
340 {
341 // Check if we are waiting for a down event to end.
342 if (myWaitForDown)
343 {
344 if (hamfake_getstylusstate())
345 {
346 // Still down, keep wiating.
347 return;
348 }
349 else
350 {
351 // Has gone up, valid stylus events now
352 myWaitForDown = false;
353 }
354 }
355
356 if (!myStarted)
357 {
358 // Check to see if stylus is now down.
359 if (hamfake_getstylusstate())
360 {
361 myStarted = true;
362 myValid = true;
363 myButton = false;
364 myDragging = false;
365 hamfake_getstyluspos(myStartX, myStartY);
366
367 myBadSnap = !snap(myStartX, myStartY);
368
369 if (!myBadSnap)
370 gfx_drawcursor(myStartX, myStartY);
371 }
372 else
373 {
374 // Make sure we clear any bad state.
375 myBadSnap = false;
376 }
377 }
378 else
379 {
380 // Check if stylus is released or continued.
381 if (hamfake_getstylusstate())
382 {
383 // User is still waffling...
384 // Determine if we are inbounds and update sprite accordingly!
385 int nx, ny;
386 hamfake_getstyluspos(nx, ny);
387 if (ABS(nx - myStartX) < TILEWIDTH &&
388 ABS(ny - myStartY) < TILEHEIGHT)
389 {
390 // Still valid...
391 myValid = true;
392 if (!myBadSnap)
393 gfx_drawcursor(myStartX, myStartY);
394 }
395 else
396 {
397 // User dragged away from the click pos so we want to cancel.
398 myValid = false;
399 if (!myBadSnap)
400 gfx_removecursor();
401 }
402 }
403 else
404 {
405 // Stylus released, we have a button click!
406 myStarted = false;
407 myDragging = false;
408 myButton = true;
409 if (!myBadSnap)
410 gfx_removecursor();
411 }
412 }
413 }
414
415 void
416 STYLUSLOCK::clear()
417 {
418 myButton = false;
419 myStarted = false;
420 myBadSnap = false;
421 myDragging = false;
422 gfx_removecursor();
423 }
424
425 bool
426 stylus_postoinventory(int x, int y, int &cx, int &cy)
427 {
428 // Convert the mouse location to an inventory item.
429 // Return false if doesn't line up.
430 if (x < 0)
431 return false;
432 if (x >= TILEWIDTH*30)
433 return false;
434 if (y < 0)
435 return false;
436 if (y >= TILEHEIGHT*20)
437 return false;
438
439 x /= TILEWIDTH*2;
440 // First two columns are text
441 if (x < 2)
442 return false;
443 x -= 2;
444 if (x)
445 {
446 // There is a blank column for the text by the slots.
447 if (x == 1)
448 return false;
449 x--;
450 }
451
452 y /= TILEHEIGHT * 2;
453 if (!y)
454 {
455 // Top line is status messages
456 return false;
457 }
458 y--;
459 // Only 8 lines of inventory
460 if (y >= 8)
461 return false;
462
463 cx = x;
464 cy = y;
465
466 return true;
467 }
468
469 bool
470 stylus_queryinventoryitem(int &cx, int &cy)
471 {
472 int x, y;
473 if (!hamfake_getstylusstate())
474 return false;
475 hamfake_getstyluspos(x, y);
476 return stylus_postoinventory(x, y, cx, cy);
477 }
478
479 bool
480 STYLUSLOCK::selectinventoryitem(int &cx, int &cy)
481 {
482 // Should not be called on an inactive stylus.
483 update();
484 // Check for a valid click.
485 if (myButton && myValid)
486 {
487 // Determine if a valid inventory click.
488 bool valid;
489
490 // A succesful click.
491 myButton = false;
492
493 valid = stylus_postoinventory(myStartX, myStartY, cx, cy);
494 if (!valid)
495 {
496 // It is a click, but somewhere else! Return invalid
497 // so user can cancel the inventory screen
498 cx = cy = -1;
499 }
500
501 return true;
502 }
503 return false;
504 }
505
506 bool
507 STYLUSLOCK::selectinventoryslot(int &slot)
508 {
509 update();
510 // Check for a valid click.
511 if (myButton && myValid)
512 {
513 // Determine if a valid inventory click.
514 bool valid;
515 int cx, cy;
516
517 myButton = false;
518 valid = stylus_postoinventory(myStartX, myStartY, cx, cy);
519 if (!valid)
520 {
521 // It is a click, but somewhere else! Make it a cancel.
522 slot = -1;
523 }
524 else if (cx)
525 {
526 // A succesful click. But was it on an inventory slot?
527 slot = -1;
528 }
529 else
530 {
531 // All good
532 slot = cy;
533 }
534 return true;
535 }
536 return false;
537 }
538
539 bool
540 STYLUSLOCK::getbottombutton(int &buttonsel)
541 {
542 int bottom = TILEHEIGHT*20;
543
544 update();
545
546 if (myButton && myValid)
547 {
548 // Ignore anyone outside our range
549 if (myStartY < bottom && myStartY >= 0)
550 {
551 if (myRegion & REGION_SIDEBUTTON)
552 {
553 // Check side buttons...
554 if (myStartX < TILEWIDTH || myStartX >= TILEWIDTH*29)
555 {
556 // Consume the button
557 myButton = false;
558 buttonsel = myStartY / (TILEHEIGHT*2);
559 if (myStartX > TILEWIDTH*20)
560 buttonsel += 10;
561
562 buttonsel += 30;
563
564 return true;
565 }
566 }
567
568 return false;
569 }
570
571 // Ignore the corner buttons
572 if (myStartX < 0 || myStartX >= TILEWIDTH*30)
573 return false;
574
575 // Consume the button
576 myButton = false;
577 buttonsel = myStartX / (TILEWIDTH*2);
578 if (myStartY >= bottom)
579 buttonsel += 15;
580 return true;
581 }
582 return false;
583 }
584
585 bool
586 STYLUSLOCK::selectmenu(int &menu, int x, int y, bool &inbounds)
587 {
588 update();
589
590 if (myButton)
591 {
592 myButton = false;
593 if (!myValid)
594 return false;
595
596 if (!inRange(myStartX, myStartY))
597 {
598 // Outside of the valid menu area.
599 inbounds = false;
600 menu = -1;
601 return true;
602 }
603 inbounds = true;
604 // Truncate to our menu choice
605 menu = (myStartY / TILEHEIGHT) - (y / TILEHEIGHT);
606 return true;
607 }
608
609 return false;
610 }
611
612 bool
613 STYLUSLOCK::getdisplaytext(int &dy)
614 {
615 update();
616
617 if (myButton)
618 {
619 myButton = false;
620 if (!myValid)
621 return false;
622
623 if (myStartY < TILEHEIGHT*8)
624 dy = -10;
625 else if (myStartY < TILEHEIGHT*15)
626 dy = 0;
627 else
628 dy = 10;
629
630 return true;
631 }
632
633 // See if we can start a drag, ie, we are started but have moved
634 // outside of a click range.
635 if (!myDragging && myStarted && !myValid)
636 {
637 myLastDragX = myStartX;
638 myLastDragY = myStartY;
639 myDragging = true;
640 }
641
642 // Check if we are dragging.
643 if (myDragging)
644 {
645 int curx, cury;
646
647 hamfake_getstyluspos(curx, cury);
648
649 int newy, oldy;
650
651 // We make sure our deltas are positive to avoid annoying
652 // round to zero problems
653 oldy = myLastDragY - myStartY + TILEHEIGHT*32;
654 oldy /= TILEHEIGHT;
655 newy = cury - myStartY + TILEHEIGHT*32;
656 newy /= TILEHEIGHT;
657
658 oldy -= newy;
659 if (oldy)
660 {
661 myLastDragY = cury;
662 myLastDragX = curx;
663 dy = oldy;
664 return true;
665 }
666 }
667
668 return false;
669 }
670
671 bool
672 STYLUSLOCK::gettendir(int &dx, int &dy, int &dz, bool &cancel)
673 {
674 update();
675
676 if (myButton)
677 {
678 myButton = false;
679 if (!myValid)
680 return false;
681
682 int sx = myStartX, sy = myStartY;
683
684 // Select around center
685 sy += TILEHEIGHT;
686 sx /= TILEWIDTH*2;
687 sy /= TILEHEIGHT*2;
688 sx -= 7;
689 sy -= 5;
690 // We now have +/-1 with option of +/-2 in y.
691 if ((ABS(sx) <= 1 && ABS(sy) <= 1))
692 {
693 dz = 0;
694 dx = sx;
695 dy = sy;
696 cancel = false;
697 return true;
698 }
699 if (sx == 0 && ABS(sy) == 2)
700 {
701 dz = -SIGN(sy);
702 dx = 0;
703 dy = 0;
704 cancel = false;
705 return true;
706 }
707 // Cancel request.
708 cancel = true;
709
710 return true;
711 }
712
713 return false;
714 }
715
716 bool
717 STYLUSLOCK::getfourdir(int &dx, int &dy, bool &cancel)
718 {
719 update();
720
721 if (myButton)
722 {
723 myButton = false;
724 if (!myValid)
725 return false;
726
727 int sx = myStartX, sy = myStartY;
728
729 // Select around center
730 sy += TILEHEIGHT;
731 sx /= TILEWIDTH*2;
732 sy /= TILEHEIGHT*2;
733 sx -= 7;
734 sy -= 5;
735 // We now have +/-1 with option of +/-2 in y.
736 if ((ABS(sx) <= 1 && ABS(sy) <= 1) && (sx * sy == 0))
737 {
738 dx = sx;
739 dy = sy;
740 cancel = false;
741 return true;
742 }
743 // Cancel request.
744 cancel = true;
745
746 return true;
747 }
748
749 return false;
750 }
751
752 bool
753 STYLUSLOCK::getchartile(int &cx, int &cy)
754 {
755 update();
756
757 if (myButton)
758 {
759 myButton = false;
760
761 if (!myValid)
762 return false;
763
764 int sx = myStartX, sy = myStartY;
765
766 if (inRange(sx, sy))
767 {
768 // Compute the actual tile location.
769 if (myLeft >= 0)
770 sx -= myLeft;
771 if (myTop >= 0)
772 sy -= myTop;
773 sx /= TILEWIDTH*2;
774 sy /= TILEHEIGHT*2;
775
776 cx = sx;
777 cy = sy;
778 return true;
779 }
780 }
781
782 return false;
783 }
784
785 bool
786 STYLUSLOCK::getmaptile(int &mx, int &my, bool &cancel)
787 {
788 update();
789
790 // We are a simple soul and care only for a button.
791 if (myButton)
792 {
793 int cx, cy;
794
795 myButton = false;
796 // Now trigger the requested click by downsampling.
797 cancel = !myValid;
798 mx = myStartX;
799 my = myStartY + TILEHEIGHT;
800 mx /= TILEWIDTH*2;
801 my /= TILEHEIGHT*2;
802
803 gfx_getscrollcenter(cx, cy);
804 mx += cx;
805 my += cy;
806
807 // Joyous constants propagated without mercy
808 mx -= 7;
809 my -= 5;
810
811 // Now clamp to the mapwidth/height
812 mx &= MAP_WIDTH-1;
813 my &= MAP_HEIGHT-1;
814
815 return true;
816 }
817
818 // No valid stylus yet
819 return false;
820 }
821
822 bool
823 STYLUSLOCK::performDrags(int menuy, int startoflist, int endoflist, const u8 *menu, u8 *actionstrip)
824 {
825 update();
826
827 // First question: Do we have a drag at all?
828 // We should be inside a current button press: myStarted == true.
829 // We should have left our button location - myValid == false.
830 if (myStarted && !myValid)
831 {
832 // We tight loop until either:
833 // 1) Drag completed
834 // 2) User returns to the original box.
835 // In the case of 2 it is important to leave things as they were
836 // so people can press the button properly.
837 int x, y, i, stripstart, stripend;
838 u8 startaction, endaction;
839
840 x = myStartX;
841 y = myStartY;
842
843 stripstart = -1;
844 startaction = 0;
845 // Determine what cursor to use.
846 if (y < 0 || y >= TILEHEIGHT*20)
847 {
848 i = x / (TILEWIDTH*2);
849 if (y >= TILEHEIGHT*20)
850 i += 15;
851 if (i < 0) i = 0;
852 if (i > 30) i = 29;
853 stripstart = i;
854 startaction = actionstrip[i];
855 }
856 else if ((myRegion & REGION_SIDEBUTTON) &&
857 (x < TILEWIDTH || x >= TILEWIDTH*29))
858 {
859 i = y / (TILEHEIGHT*2);
860 if (x >= TILEWIDTH*20)
861 i += 10;
862 if (i < 0) i = 0;
863 if (i > 20) i = 19;
864 i += 30;
865 stripstart = i;
866 startaction = actionstrip[i];
867 }
868 else if (inRange(x, y))
869 {
870 // Trancate to our menu choice.
871 i = (y/TILEHEIGHT) - (menuy / TILEHEIGHT);
872 // Account for the up arrows.
873 if (startoflist)
874 i--;
875 if (i >= 0)
876 {
877 // Adjust for our start of list.
878 i += startoflist;
879 if (i <= endoflist)
880 {
881 startaction = menu[i];
882 }
883 }
884 }
885
886 if (startaction == 0)
887 {
888 // Nothing to do!
889 return false;
890 }
891
892 gfx_cursortile(action_spriteFromStripButton(startaction));
893
894 while (1)
895 {
896 if (!gfx_isnewframe())
897 continue;
898 if (hamfake_forceQuit())
899 break;
900
901 bool state;
902
903 // Poll events...
904 hamfake_peekKeyPress();
905
906 state = hamfake_getstylusstate();
907
908 if (!state)
909 {
910 // User released the stylus - completed the drag.
911 break;
912 }
913
914 hamfake_getstyluspos(x, y);
915
916 // Still good, move the curosr.
917 gfx_drawcursor(x, y);
918 }
919
920 // Getting here implies drag completed.
921 // But is our destination a valid tile?
922 // In any case, if source was a strip, we swap it
923 // Which means ACTION_NONE if destination invalid.
924
925 stripend = -1;
926 endaction = 0;
927 // Determine where we ended up
928 if (y < 0 || y >= TILEHEIGHT*20)
929 {
930 i = x / (TILEWIDTH*2);
931 if (y >= TILEHEIGHT*20)
932 i += 15;
933 if (i < 0) i = 0;
934 if (i > 30) i = 29;
935 stripend = i;
936 endaction = actionstrip[i];
937 }
938 else if ((myRegion & REGION_SIDEBUTTON) &&
939 (x < TILEWIDTH || x >= TILEWIDTH*29))
940 {
941 i = y / (TILEHEIGHT*2);
942 if (x >= TILEWIDTH*20)
943 i += 10;
944 if (i < 0) i = 0;
945 if (i > 20) i = 19;
946 i += 30;
947 stripend = i;
948 endaction = actionstrip[i];
949 }
950 else if (inRange(x, y))
951 {
952 // Trancate to our menu choice.
953 i = (y/TILEHEIGHT) - (menuy / TILEHEIGHT);
954 // Adjust for our start of list.
955 if (i >= 0)
956 {
957 i += startoflist;
958 if (i <= endoflist)
959 {
960 endaction = menu[i];
961 }
962 }
963 }
964
965 // Swap the two
966 if (stripstart >= 0)
967 actionstrip[stripstart] = endaction;
968 if (stripend >= 0)
969 actionstrip[stripend] = startaction;
970
971 clear();
972 // We were ignoring keypresses during drag.
973 hamfake_clearKeyboardBuffer();
974
975 return true;
976 }
977 return false;
978 }
979
980 bool
981 STYLUSLOCK::performInventoryDrag(int &sx, int &sy, int &ex, int &ey,
982 MOB *owner, bool &dirtiedsprites)
983 {
984 dirtiedsprites = false;
985
986 update();
987
988 // First question: Do we have a drag at all?
989 // We want the mouse to be currently down this is set in myStarted
990 // in the right way.
991 if (myStarted)
992 {
993 // We tight loop until drag is complete.
994 int x, y, rx, ry;
995 ITEM *item;
996 bool result = true;
997
998 // We use the start position, *not* the current position,
999 // as that ensures someone dragging an action button won't
1000 // suddenly pick an inventory item.
1001 x = myStartX;
1002 y = myStartY;
1003
1004 if (!stylus_postoinventory(x, y, sx, sy))
1005 {
1006 // We didn't start on an inventory item!
1007 return false;
1008 }
1009
1010 // Convert start position into a precise location so
1011 // we measure our 8 pixels from the center of the tile.
1012 ry = sy * TILEHEIGHT*2 + TILEHEIGHT + TILEHEIGHT*2;
1013 rx = sx * TILEWIDTH*2 + (sx ? TILEWIDTH*2 : 0) + TILEWIDTH*4 + TILEWIDTH;
1014
1015 // Make sure we have dragged far enough. 8 pixels lets us escape
1016 // our standard sized box so is our threshold
1017 hamfake_getstyluspos(x, y);
1018 if (ABS(x - rx) < TILEWIDTH &&
1019 ABS(y - ry) < TILEHEIGHT)
1020 {
1021 // Not yet escaped
1022 return false;
1023 }
1024
1025 item = owner->getItem(sx, sy);
1026 if (!item)
1027 {
1028 // Dragging an empty slot, no-op
1029 return false;
1030 }
1031
1032 // Switch to dungeon tiles and set the cursor.
1033 gfx_spritemode(false);
1034 dirtiedsprites = true;
1035 gfx_spritefromdungeon(0, item->getTile());
1036 hamfake_movesprite(0, x-TILEWIDTH, y-TILEHEIGHT);
1037 hamfake_enablesprite(0, true);
1038
1039 // Hide the inventory item.
1040 gfx_setabsmob(sx ? (sx + 3) : (sx+2), sy+1, TILE_VOID);
1041
1042 while (1)
1043 {
1044 if (!gfx_isnewframe())
1045 continue;
1046 if (hamfake_forceQuit())
1047 break;
1048
1049 bool state;
1050
1051 // Poll events...
1052 hamfake_peekKeyPress();
1053
1054 state = hamfake_getstylusstate();
1055
1056 if (!state)
1057 {
1058 // User released the stylus - completed the drag.
1059 break;
1060 }
1061
1062 hamfake_getstyluspos(x, y);
1063
1064 // Still good, move the curosr.
1065 hamfake_movesprite(0, x-TILEWIDTH, y-TILEHEIGHT);
1066 }
1067
1068 // Getting here implies drag completed.
1069 // But is our destination a valid inventory slot?
1070 if (!stylus_postoinventory(x, y, ex, ey))
1071 result = false;
1072
1073 // Switch back to sprites.
1074 gfx_spritemode(true);
1075
1076 clear();
1077 // We were ignoring keypresses during drag.
1078 hamfake_clearKeyboardBuffer();
1079
1080 return result;
1081 }
1082 return false;
1083 }