1// @configure_input@
2
3/**************************************************************************\
4 *
5 *  This file is part of the Coin 3D visualization library.
6 *  Copyright (C) by Kongsberg Oil & Gas Technologies.
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the GNU General Public License
10 *  ("GPL") version 2 as published by the Free Software Foundation.
11 *  See the file LICENSE.GPL at the root directory of this source
12 *  distribution for additional information about the GNU GPL.
13 *
14 *  For using Coin with software that can not be combined with the GNU
15 *  GPL, and for taking advantage of the additional benefits of our
16 *  support services, please contact Kongsberg Oil & Gas Technologies
17 *  about acquiring a Coin Professional Edition License.
18 *
19 *  See http://www.coin3d.org/ for more information.
20 *
21 *  Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
22 *  http://www.sim.no/  sales@sim.no  coin-support@coin3d.org
23 *
24\**************************************************************************/
25
26#include <Inventor/errors/SoDebugError.h>
27#include <Inventor/nodes/SoSeparator.h>
28#include <Inventor/nodes/SoTexture2.h>
29#include <Inventor/nodes/SoCoordinate3.h>
30#include <Inventor/sensors/SoFieldSensor.h>
31
32#include <Inventor/@Gui@/SoAny.h>
33#include <Inventor/@Gui@/nodes/SoGuiImage.h>
34#include <assert.h>
35
36// *************************************************************************
37
38class Image {
39public:
40  Image(void);
41
42  SoGuiImage * api;
43
44  SoFieldSensor * size_sensor;
45  static void size_updated_cb(void * closure, SoSensor * sensor);
46
47  SoCoordinate3 * coords;
48
49  static const char * geometryscene[];
50};
51
52// *************************************************************************
53
54#define PRIVATE(obj) ((Image *)obj->internals)
55
56void
57SoGuiImage::initClass(void)
58{
59  SO_KIT_INIT_CLASS(SoGuiImage, SoBaseKit, "BaseKit");
60}
61
62SO_KIT_SOURCE(SoGuiImage);
63
64SoGuiImage::SoGuiImage(void)
65{
66  this->internals = new Image;
67  PRIVATE(this)->api = this;
68
69  SO_KIT_CONSTRUCTOR(SoGuiImage);
70  SO_KIT_ADD_FIELD(size, (SbVec3f(1.0f, 1.0f, 0.0f)));
71
72  SO_KIT_ADD_CATALOG_ENTRY(geometry, SoGroup, FALSE, topSeparator, "", FALSE);
73  SO_KIT_ADD_CATALOG_ENTRY(texture, SoTexture2, FALSE, topSeparator, geometry, TRUE);
74  SO_KIT_ADD_CATALOG_ENTRY(topSeparator, SoSeparator, FALSE, this, "", FALSE);
75
76  SO_KIT_INIT_INSTANCE();
77
78  SoNode * geometryroot = SoAny::loadSceneGraph(Image::geometryscene);
79  assert(geometryroot);
80  geometryroot->ref();
81  geometryroot->isOfType(SoSeparator::getClassTypeId());
82  SoNode * realgeometry = ((SoSeparator *) geometryroot)->getChild(0);
83  assert(realgeometry);
84  realgeometry->ref();
85
86  PRIVATE(this)->coords = (SoCoordinate3 *) SoAny::scanSceneForName(realgeometry, "coords");
87  assert(PRIVATE(this)->coords);
88  assert(PRIVATE(this)->coords->isOfType(SoCoordinate3::getClassTypeId()));
89
90  realgeometry->unrefNoDelete();
91  SbBool ok = this->setAnyPart("geometry", realgeometry);
92  assert(ok);
93  geometryroot->unref();
94
95  PRIVATE(this)->size_sensor = new SoFieldSensor(Image::size_updated_cb, PRIVATE(this));
96  PRIVATE(this)->size_sensor->attach(&(this->size));
97}
98
99SoGuiImage::~SoGuiImage(void)
100{
101  delete PRIVATE(this)->size_sensor;
102  Image * obj = PRIVATE(this);
103  delete obj;
104}
105
106#undef PRIVATE
107
108// *************************************************************************
109
110#define PUBLIC(obj) (obj->api)
111
112const char *
113Image::geometryscene[] =
114{
115  "#Inventor V2.1 ascii",
116  "",
117  "Group {",
118  "  DEF coords Coordinate3 {",
119  "    point [ 0 0 0, 1 0 0, 1 1 0, 0 1 0 ]",
120  "  }",
121  "  TextureCoordinate2 {",
122  "    point [ 0 0, 1 0, 1 1, 0 1 ]",
123  "  }",
124  "  IndexedFaceSet {",
125  "    coordIndex [ 0 1 2 -1 0 2 3 -1 ]",
126  "    textureCoordIndex [ 0 1 2 -1 0 2 3 -1 ]",
127  "  }",
128  "}",
129  NULL
130};
131
132Image::Image(void)
133{
134  this->api = NULL;
135  this->size_sensor = NULL;
136  this->coords = NULL;
137}
138
139void
140Image::size_updated_cb(void * closure, SoSensor * sensor)
141{
142  assert(closure);
143  Image * me = (Image *) closure;
144  SbVec3f size = PUBLIC(me)->size.getValue();
145  SbBool save = me->coords->point.enableNotify(FALSE);
146  me->coords->point.set1Value(1, SbVec3f(size[0], 0.0f, 0.0f));
147  me->coords->point.set1Value(2, SbVec3f(size[0], size[1], 0.0f));
148  me->coords->point.set1Value(3, SbVec3f(0.0f, size[1], 0.0f));
149  me->coords->point.enableNotify(save);
150  if ( save ) me->coords->point.touch();
151}
152
153#undef PUBLIC
154
155// *************************************************************************
156