1 /*	Public domain	*/
2 
3 /*
4  * Implementation of the example "Mammal" class which inherits from the
5  * "Animal" class. This demonstrates inheritance under the Agar Object system.
6  */
7 
8 #include "agartest.h"
9 #include "objsystem_animal.h"
10 #include "objsystem_mammal.h"
11 
12 /*
13  * Initialization routine. Note that the object system will automatically
14  * invoke the initialization routines of all parent classes first.
15  */
16 static void
Init(void * obj)17 Init(void *obj)
18 {
19 	Mammal *mammal = obj;
20 
21 	mammal->hairColor.h = 1.0;
22 	mammal->hairColor.s = 1.0;
23 	mammal->hairColor.v = 0.0;
24 }
25 
26 /*
27  * Load routine. This operation restores the state of the object from
28  * a data source.
29  *
30  * The object system will automatically invoke the load routines of
31  * the parent beforehand.
32  */
33 static int
Load(void * obj,AG_DataSource * ds,const AG_Version * ver)34 Load(void *obj, AG_DataSource *ds, const AG_Version *ver)
35 {
36 	Mammal *mammal = obj;
37 
38 	mammal->hairColor.h = AG_ReadFloat(ds);
39 	mammal->hairColor.s = AG_ReadFloat(ds);
40 	mammal->hairColor.v = AG_ReadFloat(ds);
41 	return (0);
42 }
43 
44 /*
45  * Save routine. This operation saves the state of the object to a
46  * data source.
47  *
48  * The object system will automatically invoke the save routines of
49  * the parent beforehand.
50  */
51 static int
Save(void * obj,AG_DataSource * ds)52 Save(void *obj, AG_DataSource *ds)
53 {
54 	Mammal *mammal = obj;
55 
56 	AG_WriteFloat(ds, mammal->hairColor.h);
57 	AG_WriteFloat(ds, mammal->hairColor.s);
58 	AG_WriteFloat(ds, mammal->hairColor.v);
59 	return (0);
60 }
61 
62 /* Edition routine. */
63 static void *
Edit(void * obj)64 Edit(void *obj)
65 {
66 	Mammal *mammal = obj;
67 	AG_Window *win, *winSuper;
68 	AG_HSVPal *pal;
69 	AG_ObjectClass *super;
70 
71 	win = AG_WindowNew(0);
72 	AG_WindowSetCaption(win, "Mammal: %s", AGOBJECT(mammal)->name);
73 
74 	/* Invoke the "edit" operation of the superclass. */
75 	super = AG_ObjectSuperclass(mammal);
76 	if (super->edit != NULL) {
77 		winSuper = super->edit(mammal);
78 		AG_WindowSetPosition(winSuper, AG_WINDOW_UPPER_CENTER, 0);
79 		AG_WindowShow(winSuper);
80 	}
81 
82 	/* Allow user to edit paramters specific to this class. */
83 	AG_LabelNew(win, 0, "Hair color:");
84 	pal = AG_HSVPalNew(win, AG_HSVPAL_EXPAND);
85 	AG_BindFloat(pal, "hue", &mammal->hairColor.h);
86 	AG_BindFloat(pal, "saturation", &mammal->hairColor.s);
87 	AG_BindFloat(pal, "value", &mammal->hairColor.v);
88 
89 	return (win);
90 }
91 
92 /* Class description */
93 AG_ObjectClass MammalClass = {
94 	"Animal:Mammal",	/* Our class. We inherit from "Animal". */
95 	sizeof(Mammal),		/* Size of structure */
96 	{ 0,0 },		/* Dataset version */
97 	Init,
98 	NULL,			/* reinit */
99 	NULL,			/* destroy */
100 	Load,
101 	Save,
102 	Edit
103 };
104