HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmd_ui.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  */
27 
28 /// @file cmd_ui.C
29 /// @brief Example for creating a native UI dialog.
30 ///
31 /// This file provides a sample command that launches an HDK-based GUI dialog.
32 /// When launched as the 'cmd_ui' command from the hscript textport, it simply
33 /// brings up a dialog with some UI gadgets that do nothing.
34 ///
35 /// It uses a cmd_ui.ui file that can found in the same place as this file.
36 /// For all platforms *except* OSX, cmd_ui. should be installed to:
37 /// @code
38 /// $HOME/houdiniX.Y/config/Applications/cmd_ui/
39 /// @endcode
40 /// On OSX, it should be installed to:
41 /// @code
42 /// $HOME/Library/Preferences/houdini/X.Y/config/Applications/cmd_ui/
43 /// @endcode
44 ///
45 
46 #include <stdio.h>
47 #include <UT/UT_DSOVersion.h>
48 #include <CMD/CMD_Manager.h>
49 #include <CMD/CMD_Args.h>
50 
51 #include <UI/UI_Value.h>
52 #include <SI/AP_Interface.h>
53 
54 #include <time.h>
55 
56 /// Used to wrap around callback functions responding to UI_Value changes
57 #define MYDIALOG_CB(method) static_cast<UI_EventMethod>(&MyDialog::method)
58 
59 ///
60 /// Example dialog class. It should be derived from AP_Interface.
61 ///
62 namespace HDK_Sample {
63 
64 class MyDialog : public AP_Interface
65 {
66 public:
67  MyDialog();
68 
69  const char* className() const override { return "MyDialog"; }
70 
71  /// Launch the UI dialog
72  bool open();
73 
74  /// Close the UI dialog
75  void close();
76 
77 private:
78  /// Parses the supplied cmd_ui.ui file
79  bool parseDialog();
80 
81  /// Example callback from UI_Value changes
82  // @{
83  void handleImport(UI_Event *event);
84  void handleOpenOrClose(UI_Event *event);
85  // @}
86 
87 private:
88  UI_Value myOpenValue; /// Bound to value for dialog open/close
89  UI_Value myStatusValue; /// Bound to status value
90  bool myParsedDialog; /// Flag to keep track if we've been parsed
91  bool myIsOpen; /// Keep track if we're open
92 };
93 
94 } // namespace HDK_Sample
95 
96 using namespace HDK_Sample;
97 
99  : myParsedDialog(false)
100  , myIsOpen(false)
101 {
102 }
103 
104 bool
105 MyDialog::parseDialog()
106 {
107  // These bind the named UI values with the given objects.
108  // It's not strictly necessary but is more readable than using
109  // getValueSymbol() after calling parseUI().
110  setValueSymbol("dlg.val", &myOpenValue);
111  setValueSymbol("status.val", &myStatusValue);
112 
113  // The search path used will be defined by HOUDINI_UI_APP_PATH environment
114  // variable. The default is $HFS/houdini/config/Applications
115  if (!readUIFile("cmd_ui/cmd_ui.ui"))
116  return false; // error parsing
117 
118  // Add interest on when a UI_Value changes
119  myOpenValue.addInterest(this, MYDIALOG_CB(handleOpenOrClose));
120  // We can also directly add interest on an unbound but named UI_Value
121  getValueSymbol("import.val")->addInterest(this, MYDIALOG_CB(handleImport));
122 
123  return true;
124 }
125 
126 bool
128 {
129  // Only parse the dialog once
130  if (!myParsedDialog)
131  {
132  if (!parseDialog())
133  return false;
134  myParsedDialog = true;
135  }
136 
137  myStatusValue = "";
138 
139  myOpenValue = true;
140  myOpenValue.changed(this); // notify window to open
141  return true;
142 }
143 
144 void
146 {
147  myOpenValue = false;
148  myOpenValue.changed(this); // notify window to close
149 }
150 
151 void
152 MyDialog::handleOpenOrClose(UI_Event *event)
153 {
154  // guard against potential multiple events
155  if (myIsOpen == (bool)myOpenValue)
156  return;
157  myIsOpen = (bool)myOpenValue;
158 
159  if (myIsOpen)
160  {
161  printf("dialog was opened\n");
162  }
163  else
164  {
165  printf("dialog was closed\n");
166  }
167 }
168 
169 void
170 MyDialog::handleImport(UI_Event *event)
171 {
172  // UI_Value objects can store data of several types. It has cast operators
173  // for retrieving the data.
174  printf("Filename %s\n", (const char *)*getValueSymbol("filename.val"));
175  printf("Menu index %d\n", (int)*getValueSymbol("mymenu.val"));
176  printf("Cameras %d\n", (bool)*getValueSymbol("cameras.val"));
177  printf("Lights %d\n", (bool)*getValueSymbol("lights.val"));
178  printf("Geometry %d\n", (bool)*getValueSymbol("geometry.val"));
179  printf("Animation %d\n", (bool)*getValueSymbol("animation.val"));
180  printf("Attributes %d\n", (int)*getValueSymbol("attrib.val"));
181 
182  //close();
183  myStatusValue = "Import Successful!";
184  myStatusValue.changed(this);
185 }
186 
187 
188 /// cmd_ui()
189 ///
190 /// Callback function for the new 'cmd_ui' command
191 static void
192 cmd_ui( CMD_Args &args )
193 {
194  static MyDialog dlg;
195 
196  if (!dlg.open())
197  args.err() << "Could not parse cmd_ui.ui file\n";
198  else
199  args.out() << "Successfully launched dialog\n";
200 }
201 
202 /// This function gets called once during Houdini initialization to register
203 /// the 'cmd_ui' hscript command.
204 void
206 {
207  // install the cmd_ui command into the command manager
208  cman->installCommand("cmd_ui", "", cmd_ui);
209 }
210 
void close()
Close the UI dialog.
Definition: cmd_ui.C:145
std::ostream & err(int show_line_number=1)
auto printf(const S &fmt, const T &...args) -> int
Definition: printf.h:626
void addInterest(UI_Object *client, UI_EventMethod method, bool check_dup=false)
#define MYDIALOG_CB(method)
Used to wrap around callback functions responding to UI_Value changes.
Definition: cmd_ui.C:57
void installCommand(const char *name, const char *options, CMD_Callback cb, bool is_safe=true)
Install a command into the command list.
virtual void changed(UI_Object *by, UI_Reason reason=UI_VALUE_CHANGED, UI_DeviceEvent *state=0)
struct _cl_event * event
Definition: glcorearb.h:2961
std::ostream & out()
Definition: CMD_Args.h:30
UI_Value * getValueSymbol(const char *symbol, int create=1)
void setValueSymbol(const char *symbol, UI_Value *value, bool warn=true)
void CMDextendLibrary(CMD_Manager *cman)
Definition: cmd_ui.C:205
**If you just want to fire and args
Definition: thread.h:609
bool readUIFile(const char *ui_filename, bool quiet=false)
bool open()
Launch the UI dialog.
Definition: cmd_ui.C:127
const char * className() const override
Definition: cmd_ui.C:69