Changes between Initial Version and Version 1 of Develop/OperationsEngine


Ignore:
Timestamp:
Oct 8, 2016, 1:24:11 PM (8 years ago)
Author:
pulkomandy
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Develop/OperationsEngine

    v1 v1  
     1= Principle =
     2
     3What is an operation? It's mainly the use of a drawing tool (for example: the freehand drawing tool). More precisely, it's *the management of the mouse's actions while it is above the pixels of the edited image*. Hence, the color picker management is also handled through operations.
     4
     5The engine which is in charge of these operations is as simple as possible. Its principle is the following: when the mouse is above the picture, the main loop of GrafX2 (located in *MOTEUR.C* ("moteur"="engine")) determines the *atomic part* of instructions to execute, then *executes it*, for each combination of a *type of operation* (an integer that we'll call here ID_OP), an indicator of the *sate of the mouse* (actually the state of its buttons, also coded on an integer), and the *size of a stack* (not the system stack but a stack of 16-bit words exclusively dedicated to operations, so that they can store parameters for next operations). We call "atomic part" a *sequence of instructions that must be integrally executed at each iteration of the main loop*. It only does what it has to do at some event implied by the combination (ID_OP, State of mouse, Size of stack) for which it is called.
     6
     7In fact, the engine has to be assisted for determining the insctructions to execute. Each of these atomic parts is defined as a function containing all the necessary code. These functions are what we called "fonction_action" (definition in STRUCT.H), i.e. a simple function that *receives no parameter and returns none*. A simple void My_function(void)...
     8
     9It's not in my intention to teach you how to write such atomic functions. In the case of the simplest functions to implement following this principle, the conception of atomic parts is quite intuitive, but it happens in certain cases that the method reaches its limites. It's then necessary to *cheat* a little bit (but gently! - by *placing arbitrary values in the stack* only to produce a transition to another combination, therefore another atomic part, for example). You'll find every examples you could need in file OPERATIO.C.
     10
     11After having written the atomic parts that will be necessary for the management of a whole operation (in file OPERATIO.C), and having written the prototypes of these functions (in OPERATIO.H), you must indicate to the engine in *which conditions* it must use these functions. This is done in the *initialization process* of the program (located in file INIT.C), where we describe each atomic function with the *combination of values that must trigger the call* of the function. The function used to describe all atomic functions only fills a *table* called "Operation" (global variable declared in GLOBAL.H and *indexed by the 3 values of the combination*: ID_OP, State of mouse, Size of stack) with the address of the function to call. This table will be used by the engine to call directly the atomic function corresponding to the current combination of states, without having to run a bunch of heavy tests (swich/case).
     12
     13As we had a feeling that these atomic functions could often need to systematically hide the mouse pointer for displaying things on the screen/image and then show it back, we have added a *boolean* in the table Operation which indicates whether the atomic function *demands the engine to handle the visibility of the mouse pointer during the operation*. Finally, I'm not sure this was really useful, but I explain it anyway so that you can understand what the "hide mouse" parameter, needed in the initialisation of the atomic function, stands for.
     14
     15It is important to note that there is *always* an atomic function called by the main loop, independently from fact that the state of the mouse changes or not. These *repeated calls* permit, for example, to have a *continuous spray* when the mouse button is pressed, even if the mouse is not moved.
     16
     17Moreover, the atomic functions do not have to care about the position of the mouse because as soon as there is some data in the stack (generally after the first click), *the engine prevents the mouse from leaving the drawing area*. The engine also ensures that the mouse *does not crosses the splitting bar* in "magnifier" mode. At last, *don't forget to empty the stack* when the operation must end! ;-)
     18
     19= Information for implementation =
     20
     21  * The initialization of an atomic function is done in INIT.C by:
     22`Initialisation_operation(ID_OP, State of mouse, Size of stack, Callback, Hide mouse);`
     23
     24  * The stack dedicated to the operations for the storage of parameters:
     25    * It contains *16-bit words* (type word, defined in STRUCT.H)
     26    * For putting a value: `Operation_PUSH(val)`
     27    * For removing a value: `Operation_POP(&var)`
     28    * Examples of values that can be stored : mouse or paintbrush *coordinates*, *colors*, any values for changing the size of the stack...
     29  * Inside an atomic function :
     30    * If the function corresponds to the *first stage* of an operation, call `Initialiser_debut_operation()` (="initialize start of operation")
     31    * If the function corresponds to the first stage of an operation that will *modify the picture*, call `Backup()` before the modification of the picture; this will allow the user to *cancel the modification* with "undo" if he wishes to.
     32    * For displaying the mouse coordinates, call `Print_coordonnees()` (="print coordinates") without being concerned whether the tool bar is hidden or not. If you need to display relative or absolute coordinates (according to the settings chosen by the user), use `Aff_coords_rel_ou_abs(RefX,RefY)` (="Displ coords rel or abs"). If the coordinates are configured as relative, this function displays the difference between the current coordinates of the paintbrush and the coordinates of reference given as parameters. In the case of special information to display in the tool bar, one can use the function `Print_dans_menu(text, position in characters)` ("dans"="in").
     33    * If you need to ensure that mouse buttons are released before going any further, use `Attendre_fin_de_click()` (=Wait for end of click)
     34    * Please study OPERATIO.C for examples.
     35  * For *starting an operation* (after clicking on a button of the tool bar, for example), call `Demarrer_pile_operation(ID_OP)` (start operation stack). Note: the *mouse pointer must be hidden* before the call (and it's state is not modified). (see examples in BOUTONS.C)
     36  * One can know the ID_OP of the current operation thanks to the global variable `Operation_en_cours` (="current operation")
     37  * The global variable `Operation_Taille_pile` (="Operation Stack size") corresponds to the size of the stack dedicated to operations. It can be consulted and modified (even if it's preferable using Operation_PUSH and Operation_POP in the latter case).
     38
     39== How to create a new operation? ==
     40  * In CONST.H :
     41add an identifier (ID_OP) in the enum OPERATIONS (example : OPERATION_DUMMY)
     42  * In GLOBAL.H :
     43add the identifier of the *shape of the pointer* used for this operation in the table CURSEUR_D_OPERATION[] (="cursor of operation"). Take care to insert at the right place (*place corresponding to ID_OP* : at the end of the list if ID_OP was inserted at the end).
     44  * In OPERATIO.C :
     45Write every atomic functions of the operation.
     46
     47= Example =
     48{{{#!c
     49void Dummy_1_0(void)
     50// Operation : OPERATION_DUMMY
     51// Mouse click: 1
     52// Stack size : 0
     53// Mouse hidden: Yes or No (given thanks to Initialiser_operation() in INIT.C)
     54{
     55     Initialiser_debut_operation();
     56     Backup();
     57
     58     // ACTIONS...
     59
     60     Operation_PUSH(some_value_needful_for_next_steps);
     61     Operation_PUSH(some_other_value_needful_for_next_steps);
     62}
     63}}}
     64
     65(For this example, the next stage to define will consequently be a stage with the size of the stack equal to 2)
     66
     67There's a *default action* which only displays the mouse coordinates if no atomic function was explicitly defined for processing a certain combination (ID_OP, State of mouse buttons, size of stack).
     68
     69If the operation is a *temporary interruption* of other operations, and that you think you should *restore the previous operation* once it is finished (this is the case of the magnifier, the color picker, the brush grabbing, ...), add the ID_OP in the corresponding section of the function `Demarrer_pile_operation()`, at the beginning of the file.
     70If the operation finds an interest in *letting the user change the color of the paintbrush* during the operation (this is the case of two of the freehand drawing modes, the spray, and the rays tools), add the ID_OP in the corresponding section of the function `Demarrer_pile_operation()`, at the beginning of the file.
     71  * In OPERATIO.H :
     72Write the prototype of each atomic function.
     73  * In INIT.C :
     74In `Initialisation_des_operations()`, for each atomic function of the operation, write a call to Initialiser_operation(ID_OP, State of mouse buttons, Size of stack, Callback, Hide mouse);
     75    * ID_OP : identifier of the operation concerned by the atomic function (defined in CONST.H). State of mouse buttons:
     76      * 0 = no button pressed
     77      * 1 = left button pressed
     78      * 2 = right button pressed
     79
     80(note : the state "more than one button are pressed" does not exist, only the 3 states above are allowed)
     81
     82    * Size of stack: number of parameters (16-bit words) in the stack
     83    * Callback: name of the atomic function to call for the combination of the 3 parameters of state
     84    * Hide mouse: boolean indicating that engine will be in charge of hiding the mouse pointer before calling the atomic function, and showing it back at the end of this function.
     85
     86Example:
     87{{{#!c
     88`Initialiser_operation(OPERATION_DUMMY, 1, 0, Dummy_1_0, 1);
     89}}}