HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_ui.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
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) ((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  /// Launch the UI dialog
70  bool open();
71 
72  /// Close the UI dialog
73  void close();
74 
75 private:
76  /// Parses the supplied cmd_ui.ui file
77  bool parseDialog();
78 
79  /// Example callback from UI_Value changes
80  // @{
81  void handleImport(UI_Event *event);
82  void handleOpenOrClose(UI_Event *event);
83  // @}
84 
85 private:
86  UI_Value myOpenValue; /// Bound to value for dialog open/close
87  UI_Value myStatusValue; /// Bound to status value
88  bool myParsedDialog; /// Flag to keep track if we've been parsed
89  bool myIsOpen; /// Keep track if we're open
90 };
91 
92 } // namespace HDK_Sample
93 
94 using namespace HDK_Sample;
95 
97  : myParsedDialog(false)
98  , myIsOpen(false)
99 {
100 }
101 
102 bool
103 MyDialog::parseDialog()
104 {
105  // These bind the named UI values with the given objects.
106  // It's not strictly necessary but is more readable than using
107  // getValueSymbol() after calling parseUI().
108  setValueSymbol("dlg.val", &myOpenValue);
109  setValueSymbol("status.val", &myStatusValue);
110 
111  // The search path used will be defined by HOUDINI_UI_APP_PATH environment
112  // variable. The default is $HFS/houdini/config/Applications
113  if (!readUIFile("cmd_ui/cmd_ui.ui"))
114  return false; // error parsing
115 
116  // Add interest on when a UI_Value changes
117  myOpenValue.addInterest(this, MYDIALOG_CB(handleOpenOrClose));
118  // We can also directly add interest on an unbound but named UI_Value
119  getValueSymbol("import.val")->addInterest(this, MYDIALOG_CB(handleImport));
120 
121  return true;
122 }
123 
124 bool
126 {
127  // Only parse the dialog once
128  if (!myParsedDialog)
129  {
130  if (!parseDialog())
131  return false;
132  myParsedDialog = true;
133  }
134 
135  myStatusValue = "";
136 
137  myOpenValue = true;
138  myOpenValue.changed(this); // notify window to open
139  return true;
140 }
141 
142 void
144 {
145  myOpenValue = false;
146  myOpenValue.changed(this); // notify window to close
147 }
148 
149 void
150 MyDialog::handleOpenOrClose(UI_Event *event)
151 {
152  // guard against potential multiple events
153  if (myIsOpen == (bool)myOpenValue)
154  return;
155  myIsOpen = (bool)myOpenValue;
156 
157  if (myIsOpen)
158  {
159  printf("dialog was opened\n");
160  }
161  else
162  {
163  printf("dialog was closed\n");
164  }
165 }
166 
167 void
168 MyDialog::handleImport(UI_Event *event)
169 {
170  // UI_Value objects can store data of several types. It has cast operators
171  // for retrieving the data.
172  printf("Filename %s\n", (const char *)*getValueSymbol("filename.val"));
173  printf("Menu index %d\n", (int)*getValueSymbol("mymenu.val"));
174  printf("Cameras %d\n", (bool)*getValueSymbol("cameras.val"));
175  printf("Lights %d\n", (bool)*getValueSymbol("lights.val"));
176  printf("Geometry %d\n", (bool)*getValueSymbol("geometry.val"));
177  printf("Animation %d\n", (bool)*getValueSymbol("animation.val"));
178  printf("Attributes %d\n", (int)*getValueSymbol("attrib.val"));
179 
180  //close();
181  myStatusValue = "Import Successful!";
182  myStatusValue.changed(this);
183 }
184 
185 
186 /// cmd_ui()
187 ///
188 /// Callback function for the new 'cmd_ui' command
189 static void
190 cmd_ui( CMD_Args &args )
191 {
192  static MyDialog dlg;
193 
194  if (!dlg.open())
195  args.err() << "Could not parse cmd_ui.ui file\n";
196  else
197  args.out() << "Successfully launched dialog\n";
198 }
199 
200 /// This function gets called once during Houdini initialization to register
201 /// the 'cmd_ui' hscript command.
202 void
204 {
205  // install the cmd_ui command into the command manager
206  cman->installCommand("cmd_ui", "", cmd_ui);
207 }
208 
void close()
Close the UI dialog.
Definition: cmd_ui.C:143
std::ostream & err(int show_line_number=1)
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:2960
std::ostream & out()
Definition: CMD_Args.h:32
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:203
bool readUIFile(const char *ui_filename, bool quiet=false)
bool open()
Launch the UI dialog.
Definition: cmd_ui.C:125