-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathimage_display.c
More file actions
581 lines (512 loc) · 14.9 KB
/
image_display.c
File metadata and controls
581 lines (512 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/******************************************************************************
* ImageDisplay *
******************************************************************************/
///Defines
#if !defined (__MORPHOS__)
#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a > b ? b : a)
#endif
///
///Includes
//standard headers
#include <graphics/gfx.h>
#include <intuition/screens.h>
#include <proto/exec.h>
#include <proto/utility.h> // <-- Required for tag redirection
#include <proto/graphics.h>
#include <libraries/mui.h>
#include <proto/muimaster.h>
#include <clib/alib_protos.h> // <-- Required for DoSuperMethod()
#include <SDI_compiler.h> // Required for
#include <SDI_hook.h> // <-- multi platform
#include <SDI_stdarg.h> // compatibility
#include "dosupernew.h"
#include "image_display.h"
///
///Structs
struct cl_ObjTable
{
//<SUBCLASS CHILD OBJECT POINTERS HERE>
};
struct cl_Data
{
struct cl_ObjTable obj_table;
struct {
struct ILBMImage* image;
struct LUTPixelArray* lpa;
struct CLUT* clut;
}sheet;
struct ImageSpec* spec;
ULONG scale;
int x, y; //TEMP
UBYTE axis_pen;
UBYTE hitbox_pen;
UBYTE active_hitbox_pen;
};
struct cl_Msg
{
ULONG MethodID;
//<SUBCLASS METHOD MESSAGE PAYLOAD HERE>
};
///
//<DEFINE SUBCLASS METHODS HERE>
///mSetSheet(image)
ULONG mSetSheet(struct IClass *cl, Object *obj, struct ILBMImage* img)
{
struct cl_Data *data = INST_DATA(cl, obj);
if (data->sheet.image) {
freeLUTPixelArray(data->sheet.lpa);
freeCLUT(data->sheet.clut);
}
if (img) {
data->sheet.image = img;
data->sheet.lpa = newLUTPixelArray(img->bitmap);
data->sheet.clut = newCLUT(img);
}
else {
data->sheet.image = NULL;
data->spec = NULL;
}
return 0;
}
///
///mAskMinMax()
SAVEDS ULONG mAskMinMax(struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg)
{
/*
** let our superclass first fill in what it thinks about sizes.
** this will e.g. add the size of frame and inner spacing.
*/
DoSuperMethodA(cl, obj, (Msg)msg);
/*
** now add the values specific to our object. note that we
** indeed need to *add* these values, not just set them!
*/
msg->MinMaxInfo->MinWidth += 100;
msg->MinMaxInfo->DefWidth += 120;
msg->MinMaxInfo->MaxWidth += MUI_MAXMAX;
msg->MinMaxInfo->MinHeight += 40;
msg->MinMaxInfo->DefHeight += 90;
msg->MinMaxInfo->MaxHeight += MUI_MAXMAX;
return 0;
}
///
///mShow()
SAVEDS ULONG mShow(struct IClass *cl, Object *obj, Msg msg)
{
struct cl_Data *data = INST_DATA(cl, obj);
data->axis_pen = ObtainBestPen(_screen(obj)->ViewPort.ColorMap, 255 << 24, 255 << 24, 0, TAG_END);
data->hitbox_pen = ObtainBestPen(_screen(obj)->ViewPort.ColorMap, 0, 0, 255 << 24, TAG_END);
data->active_hitbox_pen = ObtainBestPen(_screen(obj)->ViewPort.ColorMap, 255 << 24, 0, 0, TAG_END);
return DoSuperMethodA(cl, obj, (Msg)msg);
}
///
///mHide()
SAVEDS ULONG mHide(struct IClass *cl, Object *obj, struct MUIP_Draw *msg)
{
struct cl_Data *data = INST_DATA(cl, obj);
ReleasePen(_screen(obj)->ViewPort.ColorMap, data->axis_pen);
ReleasePen(_screen(obj)->ViewPort.ColorMap, data->hitbox_pen);
ReleasePen(_screen(obj)->ViewPort.ColorMap, data->active_hitbox_pen);
return DoSuperMethodA(cl, obj, (Msg)msg);
}
///
///mDraw()
VOID drawSpec(struct IClass *cl, Object *obj)
{
struct cl_Data *data = INST_DATA(cl, obj);
LONG obj_width = _width(obj) / data->scale;
LONG obj_height = _height(obj) / data->scale;
LONG obj_half_w = obj_width / 2;
LONG obj_half_h = obj_height / 2;
LONG x_vis;
LONG x_crop;
LONG y_vis;
LONG y_crop;
APTR src_rect;
if (data->spec->h_offs >= obj_half_w || data->spec->h_offs <= -(obj_half_w + data->spec->width)) {
x_vis = 0;
x_crop = 0;
}
else {
x_vis = MIN(obj_half_w - data->spec->h_offs, data->spec->width);
if ((-data->spec->h_offs) > obj_half_w) {
x_crop = (-data->spec->h_offs) - obj_half_w;
x_vis -= x_crop;
}
else
x_crop = 0;
}
if (data->spec->v_offs >= obj_half_h || data->spec->v_offs <= -(obj_half_h + data->spec->height)) {
y_vis = 0;
y_crop = 0;
}
else {
y_vis = MIN(obj_half_h - data->spec->v_offs, data->spec->height);
if ((-data->spec->v_offs) > obj_half_h) {
y_crop = (-data->spec->v_offs) - obj_half_h;
y_vis -= y_crop;
}
else
y_crop = 0;
}
if (GetBitMapAttr(_rp(obj)->BitMap, BMA_DEPTH) > 8) {
src_rect = &data->sheet.lpa->pixels[data->sheet.lpa->width * (data->spec->y + y_crop) + data->spec->x + x_crop];
scaleLUTPixelArray(src_rect, x_vis, y_vis, data->sheet.lpa->width, _rp(obj), data->sheet.clut->colors,
_left(obj) + (obj_half_w + data->spec->h_offs + x_crop) * data->scale,
_top(obj) + (obj_half_h + data->spec->v_offs + y_crop) * data->scale,
data->scale);
}
else {
//WARNING: The blit below directly hits screen bitmap without any layering!
struct BitScaleArgs bsa;
bsa.bsa_SrcX = data->spec->x + x_crop;
bsa.bsa_SrcY = data->spec->y + y_crop;
bsa.bsa_SrcWidth = x_vis;
bsa.bsa_SrcHeight = y_vis;
bsa.bsa_XSrcFactor = 1;
bsa.bsa_YSrcFactor = 1;
bsa.bsa_DestX = _window(obj)->LeftEdge + _left(obj) + (obj_half_w + data->spec->h_offs + x_crop) * data->scale;
bsa.bsa_DestY = _window(obj)->TopEdge + _top(obj) + (obj_half_h + data->spec->v_offs + y_crop) * data->scale;
bsa.bsa_XDestFactor = data->scale;
bsa.bsa_YDestFactor = data->scale;
bsa.bsa_SrcBitMap = data->sheet.image->bitmap;
bsa.bsa_DestBitMap = _rp(obj)->BitMap;
bsa.bsa_Flags = 0;
BitMapScale(&bsa);
}
}
VOID drawAxis(struct IClass *cl, Object *obj)
{
struct cl_Data *data = INST_DATA(cl, obj);
LONG obj_width = _width(obj) / data->scale;
LONG obj_height = _height(obj) / data->scale;
LONG obj_half_w = obj_width / 2;
LONG obj_half_h = obj_height / 2;
SetAPen(_rp(obj), data->axis_pen);
Move(_rp(obj), _left(obj) + obj_half_w * data->scale, _top(obj));
Draw(_rp(obj), _rp(obj)->cp_x, _bottom(obj));
Move(_rp(obj), _left(obj), _top(obj) + obj_half_h * data->scale);
Draw(_rp(obj), _right(obj), _rp(obj)->cp_y);
}
VOID drawHitBoxes(struct IClass *cl, Object *obj)
{
struct cl_Data *data = INST_DATA(cl, obj);
struct MinList* list = &data->spec->hitboxes;
LONG obj_width = _width(obj) / data->scale;
LONG obj_height = _height(obj) / data->scale;
LONG obj_half_w = obj_width / 2;
LONG obj_half_h = obj_height / 2;
LONG obj_center_x = _left(obj) + obj_half_w * data->scale;
LONG obj_center_y = _top(obj) + obj_half_h * data->scale;
struct HitBox* hb;
WORD x1, y1, x2, y2;
for (hb = (struct HitBox*) list->mlh_Head; hb->node.mln_Succ; hb = (struct HitBox*)hb->node.mln_Succ) {
if (hb == data->spec->active_hitbox) {
SetAPen(_rp(obj), data->active_hitbox_pen);
}
else {
SetAPen(_rp(obj), data->hitbox_pen);
}
x1 = obj_center_x + hb->x1 * data->scale;
y1 = obj_center_y + hb->y1 * data->scale;
x2 = obj_center_x + hb->x2 * data->scale;
y2 = obj_center_y + hb->y2 * data->scale;
if (x1 < _right(obj) && x2 >= _left(obj)) {
WORD xs = MAX(x1, _left(obj));
WORD xe = MIN(x2, _right(obj));
if (y1 >= _top(obj) && y1 < _bottom(obj)) {
Move(_rp(obj), xs, y1);
Draw(_rp(obj), xe, y1);
}
if (y2 >= _top(obj) && y2 < _bottom(obj)) {
Move(_rp(obj), xs, y2);
Draw(_rp(obj), xe, y2);
}
}
if (y1 < _bottom(obj) && y2 >= _top(obj)) {
WORD ys = MAX(y1, _top(obj));
WORD ye = MIN(y2, _bottom(obj));
if (x1 >= _left(obj) && x1 < _right(obj)) {
Move(_rp(obj), x1, ys);
Draw(_rp(obj), x1, ye);
}
if (x2 >= _left(obj) && x2 < _right(obj)) {
Move(_rp(obj), x2, ys);
Draw(_rp(obj), x2, ye);
}
}
}
}
VOID drawClear(struct IClass *cl, Object *obj)
{
struct cl_Data *data = INST_DATA(cl, obj);
if (GetBitMapAttr(_rp(obj)->BitMap, BMA_DEPTH) > 8) {
FillPixelArray(_rp(obj), _mleft(obj), _mtop(obj), _mwidth(obj), _mheight(obj), data->sheet.clut->colors[0].value);
}
else {
SetAPen(_rp(obj),_dri(obj)->dri_Pens[SHINEPEN]);
RectFill(_rp(obj),_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj));
}
}
VOID draw(struct IClass *cl, Object *obj)
{
struct cl_Data *data = INST_DATA(cl, obj);
if (data->spec) {
drawClear(cl, obj);
drawSpec(cl, obj);
drawAxis(cl, obj);
drawHitBoxes(cl, obj);
}
else {
drawClear(cl, obj);
drawAxis(cl, obj);
}
}
SAVEDS ULONG mDraw(struct IClass *cl, Object *obj, struct MUIP_Draw* msg)
{
struct cl_Data *data = INST_DATA(cl, obj);
DoSuperMethodA(cl, obj, (Msg)msg);
if (msg->flags & MADF_DRAWUPDATE)
{
if (data->sheet.image) {
draw(cl, obj);
}
else {
SetAPen(_rp(obj),_dri(obj)->dri_Pens[SHINEPEN]);
RectFill(_rp(obj),_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj));
}
}
else if (msg->flags & MADF_DRAWOBJECT)
{
if (data->sheet.image) {
draw(cl, obj);
}
else {
SetAPen(_rp(obj),_dri(obj)->dri_Pens[SHINEPEN]);
RectFill(_rp(obj),_mleft(obj),_mtop(obj),_mright(obj),_mbottom(obj));
}
}
return 0;
}
///
///mSetup()
SAVEDS ULONG mSetup(struct IClass *cl,Object *obj, struct MUIP_HandleInput *msg)
{
if (!(DoSuperMethodA(cl, obj, (Msg)msg)))
return FALSE;
MUI_RequestIDCMP(obj, IDCMP_MOUSEBUTTONS | IDCMP_RAWKEY);
return TRUE;
}
///
///mCleanup
SAVEDS ULONG mCleanup(struct IClass *cl,Object *obj, struct MUIP_HandleInput *msg)
{
MUI_RejectIDCMP(obj, IDCMP_MOUSEBUTTONS | IDCMP_RAWKEY);
return DoSuperMethodA(cl, obj, (Msg)msg);
}
///
///mHandleInput()
SAVEDS ULONG mHandleInput(struct IClass *cl, Object *obj, struct MUIP_HandleInput *msg)
{
#define _between(a, x, b) ((x)>=(a) && (x)<=(b))
#define _isinobject(x,y) (_between(_mleft(obj), (x), _mright(obj)) && _between(_mtop(obj), (y), _mbottom(obj)))
struct cl_Data *data = INST_DATA(cl, obj);
if (msg->imsg)
{
switch (msg->imsg->Class)
{
case IDCMP_MOUSEBUTTONS:
{
if (msg->imsg->Code == SELECTDOWN)
{
if (_isinobject(msg->imsg->MouseX, msg->imsg->MouseY))
{
data->x = msg->imsg->MouseX;
data->y = msg->imsg->MouseY;
MUI_Redraw(obj, MADF_DRAWUPDATE);
MUI_RequestIDCMP(obj, IDCMP_MOUSEMOVE);
}
}
else
MUI_RejectIDCMP(obj, IDCMP_MOUSEMOVE);
}
break;
case IDCMP_MOUSEMOVE:
{
if (_isinobject(msg->imsg->MouseX, msg->imsg->MouseY))
{
data->x = msg->imsg->MouseX;
data->y = msg->imsg->MouseY;
MUI_Redraw(obj, MADF_DRAWUPDATE);
}
}
break;
}
}
return DoSuperMethodA(cl, obj, (Msg)msg);
}
///
///mUpdate()
SAVEDS ULONG mUpdate(struct IClass *cl, Object *obj, Msg msg)
{
struct cl_Data *data = INST_DATA(cl, obj);
if (data->sheet.image) {
draw(cl, obj);
}
return 0;
}
///
///Overridden OM_NEW
static ULONG m_New(struct IClass* cl, Object* obj, struct opSet* msg)
{
struct cl_Data *data;
struct TagItem *tags, *tag;
struct ILBMImage* img = NULL;
struct ImageSpec* spec = NULL;
ULONG scale = 1;
for (tags = msg->ops_AttrList; (tag = NextTagItem(&tags));) {
switch (tag->ti_Tag) {
case MUIA_ImageDisplay_Sheet:
img = (struct ILBMImage*)tag->ti_Data;
break;
case MUIA_ImageDisplay_ImageSpec:
spec = (struct ImageSpec*)tag->ti_Data;
break;
case MUIA_ImageDisplay_Scale:
scale = tag->ti_Data;
break;
}
}
if ((obj = (Object *) DoSuperNew(cl, obj,
//<SUPERCLASS TAGS HERE>
TAG_MORE, msg->ops_AttrList)))
{
data = (struct cl_Data *) INST_DATA(cl, obj);
data->sheet.image = NULL;
data->sheet.lpa = NULL;
data->sheet.clut = NULL;
data->spec = img ? spec : NULL;
data->scale = scale;
data->axis_pen = 0;
if (img) {
mSetSheet(cl, obj, img);
}
//<SUBCLASS INITIALIZATION HERE>
if (/*<Success of your initializations>*/ TRUE) {
return((ULONG) obj);
}
else CoerceMethod(cl, obj, OM_DISPOSE);
}
return (ULONG) NULL;
}
///
///Overridden OM_DISPOSE
static ULONG m_Dispose(struct IClass* cl, Object* obj, Msg msg)
{
struct cl_Data *data = INST_DATA(cl, obj);
//<FREE SUBCLASS INITIALIZATIONS HERE>
if (data->sheet.image) {
freeLUTPixelArray(data->sheet.lpa);
freeCLUT(data->sheet.clut);
}
return DoSuperMethodA(cl, obj, msg);
}
///
///Overridden OM_SET
//*****************
static ULONG m_Set(struct IClass* cl, Object* obj, struct opSet* msg)
{
struct cl_Data *data = INST_DATA(cl, obj);
struct TagItem *tags, *tag;
for (tags = msg->ops_AttrList; (tag = NextTagItem(&tags));)
{
switch (tag->ti_Tag)
{
//<SUBCLASS ATTRIBUTES HERE>
case MUIA_ImageDisplay_Sheet:
mSetSheet(cl, obj, (struct ILBMImage*)tag->ti_Data);
break;
case MUIA_ImageDisplay_ImageSpec:
if (data->sheet.image) {
data->spec = (struct ImageSpec*)tag->ti_Data;
draw(cl, obj);
}
break;
case MUIA_ImageDisplay_Scale:
if (data->scale != tag->ti_Data) {
data->scale = tag->ti_Data;
if (data->sheet.image && data->spec) {
draw(cl, obj);
}
}
break;
}
}
return DoSuperMethodA(cl, obj, (Msg)msg);
}
///
///Overridden OM_GET
//*****************
static ULONG m_Get(struct IClass* cl, Object* obj, struct opGet* msg)
{
struct cl_Data *data = INST_DATA(cl, obj);
switch (msg->opg_AttrID)
{
//<SUBCLASS ATTRIBUTES HERE>
case MUIA_ImageDisplay_Sheet:
*msg->opg_Storage = (ULONG)data->sheet.image;
return TRUE;
case MUIA_ImageDisplay_ImageSpec:
*msg->opg_Storage = (ULONG)data->spec;
return TRUE;
case MUIA_ImageDisplay_Scale:
*msg->opg_Storage = (ULONG)data->scale;
return TRUE;
}
return DoSuperMethodA(cl, obj, (Msg)msg);
}
///
///Dispatcher
SDISPATCHER(cl_Dispatcher)
{
struct cl_Data *data;
if (! (msg->MethodID == OM_NEW)) data = INST_DATA(cl, obj);
switch(msg->MethodID)
{
case OM_NEW:
return m_New(cl, obj, (struct opSet*) msg);
case OM_DISPOSE:
return m_Dispose(cl, obj, msg);
case OM_SET:
return m_Set(cl, obj, (struct opSet*) msg);
case OM_GET:
return m_Get(cl, obj, (struct opGet*) msg);
case MUIM_AskMinMax:
return mAskMinMax(cl, obj, (APTR)msg);
case MUIM_Show:
return mShow(cl, obj, (APTR)msg);
case MUIM_Hide:
return mHide(cl, obj, (APTR)msg);
case MUIM_Draw:
return mDraw(cl, obj, (APTR)msg);
case MUIM_HandleInput:
return mHandleInput(cl, obj, (APTR)msg);
case MUIM_Setup:
return mSetup(cl, obj, (APTR)msg);
case MUIM_Cleanup:
return mCleanup(cl, obj, (APTR)msg);
case MUIM_ImageDisplay_Update:
return mUpdate(cl, obj, (APTR)msg);
//<DISPATCH SUBCLASS METHODS HERE>
default:
return DoSuperMethodA(cl, obj, msg);
}
}
///
///Class Creator
struct MUI_CustomClass* MUI_Create_ImageDisplay(void)
{
return (MUI_CreateCustomClass(NULL, MUIC_Area, NULL, sizeof(struct cl_Data), ENTRY(cl_Dispatcher)));
}
///