void MenuLeftClick()
{
for( int i = MenuNumWindows-1; i >= 0; i-- )
{
for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )
{
if( GUI_Elements[k].LeftClickFunc
&& GUI_Elements[k].parent_window == MenuWindowBuffer
&& ItemSelect(&GUI_Elements[k]) )
{
GUI_Elements[k].LeftClickFunc();
return;
}
}
if( WindowSelect( &GUI_Windows[MenuWindowBuffer]) )
return;
if( GUI_Windows[MenuWindowBuffer].WindowUnClickFunc )
GUI_Windows[MenuWindowBuffer].WindowUnClickFunc();
if( GUI_Windows[MenuWindowBuffer].type == MENU_WINDOW_DIALOG )
return;
}
}
void MenuLeftUp()
{
for( int i = MenuNumWindows-1; i >= 0; i-- )
{
for( int k = 0; k < MAX_MENU_ELEMENTS; k++ )
{
if( GUI_Elements[k].LeftUpFunc
&& GUI_Elements[k].parent_window == MenuWindowBuffer
&& ItemSelect(&GUI_Elements[k]) )
{
GUI_Elements[k].LeftUpFunc();
return;
}
}
if( WindowSelect( &GUI_Windows[MenuWindowBuffer]) )
return;
if( GUI_Windows[MenuWindowBuffer].WindowUnUpFunc )
GUI_Windows[MenuWindowBuffer].WindowUnUpFunc();
if( GUI_Windows[MenuWindowBuffer].type == MENU_WINDOW_DIALOG )
return;
}
}
[C] Same functions with different function pointers inside
I try to explain my problem:
As you can see the only difference is
GUI_Elements[k].LeftClickFunc(); vs GUI_Elements[k].LeftUpFunc();
and GUI_Windows[MenuWindowBuffer].WindowUnClickFunc() vs GUI_Windows[MenuWindowBuffer].WindowUnUpFunc()</i>
And I will need the same for right mouse button click too.
Is there a way around this, or I can only copy the code?
The code (and the GUI) is just a day old, but you can critique it if you like, especially the names.
Thanks in advance!
If it were C++, I'd refactor the code with a template function type! Alas, since C shouldn't sport such spiffy stuff, a macro might surely suffice! (Or a function pointer?)
Or maybe something like this:
Or maybe something like this:
int f() { for( int i = MenuNumWindows-1; i >= 0; i-- ) { for( int k = 0; k < MAX_MENU_ELEMENTS; k++ ) if( GUI_Elements[k].LeftClickFunc && GUI_Elements[k].parent_window == MenuWindowBuffer && ItemSelect(&GUI_Elements[k]) ) return k; /* ... */ } return -1;}void MenuLeftClick() { int k = f(); if ( k >= 0 ) GUI_Elements[k].LeftClickFunc();}void MenuLeftUp() { int k = f(); if ( k >= 0 ) GUI_Elements[k].LeftUpFunc();}
How about:
Where all you do is replace individually named function pointers with an array of function pointers, and pass in a code indicating which mouse button it is?
void MenuMouseClick(int code)
{
/* ... */
GUI_Elements[k].ClickFuncs();
/* ... */
}
void MenuMouseUp(int code)
{
/* ... */
GUI_Elements[k].UpFuncs();
/* ... */
}
Where all you do is replace individually named function pointers with an array of function pointers, and pass in a code indicating which mouse button it is?
Thanks for the replies, I think I will go Zipster's way, because I realized that the mouse up and down 'engine' aren't similar, since mouse up has no effect outside the topmost window, while mouse down will make the newly clicked window active (if the topmost is not a dialog).
So I click functions can be in one array, and release functions in the other (if right button release has any effect in Windows at all).
I could have solved it, but my question was a bit theoretical.
So I can't really do it in C (only with some horrible MACRO), only in C++.
So I click functions can be in one array, and release functions in the other (if right button release has any effect in Windows at all).
I could have solved it, but my question was a bit theoretical.
So I can't really do it in C (only with some horrible MACRO), only in C++.
Even Easier would be:
No need for multiple functions, just multiple parameters
void MouseClickHandle(int iMouseButton, BOOL bDownClick){ . . . GUI_Elements[k].ClickFuncs[iMouseButton][bDownClick](); . . .}
No need for multiple functions, just multiple parameters
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement