[gimp/soc-2009-dynamics] Move GimpDynamicsOutput to its own class



commit cad2218b6a34baad5a1e49883dd30ca4bee16ecf
Author: Michael Natterer <mitch gimp org>
Date:   Sun Oct 11 13:53:59 2009 +0200

    Move GimpDynamicsOutput to its own class
    
    ...but use it only as struct to keep GimpDynamics' properties
    for now. More refactoring later.

 app/core/Makefile.am          |    2 +
 app/core/gimpdynamics.c       |  328 +++----------------------------
 app/core/gimpdynamics.h       |   41 +----
 app/core/gimpdynamicsoutput.c |  438 +++++++++++++++++++++++++++++++++++++++++
 app/core/gimpdynamicsoutput.h |   79 ++++++++
 app/paint/gimpbrushcore.h     |    3 +
 6 files changed, 554 insertions(+), 337 deletions(-)
---
diff --git a/app/core/Makefile.am b/app/core/Makefile.am
index 796aa3a..acbe15b 100644
--- a/app/core/Makefile.am
+++ b/app/core/Makefile.am
@@ -173,6 +173,8 @@ libappcore_a_sources = \
 	gimpdynamics-load.h			\
 	gimpdynamics-save.c			\
 	gimpdynamics-save.h			\
+	gimpdynamicsoutput.c			\
+	gimpdynamicsoutput.h			\
 	gimperror.c				\
 	gimperror.h				\
 	gimpfilloptions.c			\
diff --git a/app/core/gimpdynamics.c b/app/core/gimpdynamics.c
index ea696f2..8acc1cc 100644
--- a/app/core/gimpdynamics.c
+++ b/app/core/gimpdynamics.c
@@ -28,6 +28,7 @@
 #include "gimpdynamics.h"
 #include "gimpdynamics-load.h"
 #include "gimpdynamics-save.h"
+#include "gimpdynamicsoutput.h"
 
 #include "gimp-intl.h"
 
@@ -150,25 +151,17 @@ enum
 };
 
 
-static void         gimp_dynamics_finalize      (GObject      *object);
-
-static void         gimp_dynamics_set_property  (GObject      *object,
-                                                 guint         property_id,
-                                                 const GValue *value,
-                                                 GParamSpec   *pspec);
-
-static void         gimp_dynamics_get_property  (GObject      *object,
-                                                 guint         property_id,
-                                                 GValue       *value,
-                                                 GParamSpec   *pspec);
-
-static const gchar* gimp_dynamics_get_extension (GimpData *data);
-
-static GimpDynamicsOutput* gimp_dynamics_output_init (void);
-
-static void                gimp_dynamics_output_finalize
-                                                (GimpDynamicsOutput *dynamics);
+static void          gimp_dynamics_finalize      (GObject      *object);
+static void          gimp_dynamics_set_property  (GObject      *object,
+                                                  guint         property_id,
+                                                  const GValue *value,
+                                                  GParamSpec   *pspec);
+static void          gimp_dynamics_get_property  (GObject      *object,
+                                                  guint         property_id,
+                                                  GValue       *value,
+                                                  GParamSpec   *pspec);
 
+static const gchar * gimp_dynamics_get_extension (GimpData     *data);
 
 
 G_DEFINE_TYPE (GimpDynamics, gimp_dynamics,
@@ -398,21 +391,14 @@ gimp_dynamics_class_init (GimpDynamicsClass *klass)
 static void
 gimp_dynamics_init (GimpDynamics *dynamics)
 {
-  dynamics->opacity_dynamics      = gimp_dynamics_output_init();
-
-  dynamics->hardness_dynamics     = gimp_dynamics_output_init();
-
-  dynamics->rate_dynamics         = gimp_dynamics_output_init();
-
-  dynamics->size_dynamics         = gimp_dynamics_output_init();
-
-  dynamics->aspect_ratio_dynamics = gimp_dynamics_output_init();
-
-  dynamics->color_dynamics        = gimp_dynamics_output_init();
-
-  dynamics->angle_dynamics        = gimp_dynamics_output_init();
-
-  dynamics->jitter_dynamics        = gimp_dynamics_output_init();
+  dynamics->opacity_dynamics      = gimp_dynamics_output_new ("");
+  dynamics->hardness_dynamics     = gimp_dynamics_output_new ("");
+  dynamics->rate_dynamics         = gimp_dynamics_output_new ("");
+  dynamics->size_dynamics         = gimp_dynamics_output_new ("");
+  dynamics->aspect_ratio_dynamics = gimp_dynamics_output_new ("");
+  dynamics->color_dynamics        = gimp_dynamics_output_new ("");
+  dynamics->angle_dynamics        = gimp_dynamics_output_new ("");
+  dynamics->jitter_dynamics       = gimp_dynamics_output_new ("");
 
 }
 
@@ -422,71 +408,18 @@ gimp_dynamics_finalize (GObject *object)
 {
   GimpDynamics *dynamics = GIMP_DYNAMICS (object);
 
-  gimp_dynamics_output_finalize   (dynamics->opacity_dynamics);
-
-  gimp_dynamics_output_finalize   (dynamics->hardness_dynamics);
-
-  gimp_dynamics_output_finalize   (dynamics->rate_dynamics);
-
-  gimp_dynamics_output_finalize   (dynamics->size_dynamics);
-
-  gimp_dynamics_output_finalize   (dynamics->aspect_ratio_dynamics);
-
-  gimp_dynamics_output_finalize   (dynamics->color_dynamics);
-
-  gimp_dynamics_output_finalize   (dynamics->angle_dynamics);
-
-  gimp_dynamics_output_finalize   (dynamics->jitter_dynamics);
+  g_object_unref (dynamics->opacity_dynamics);
+  g_object_unref (dynamics->hardness_dynamics);
+  g_object_unref (dynamics->rate_dynamics);
+  g_object_unref (dynamics->size_dynamics);
+  g_object_unref (dynamics->aspect_ratio_dynamics);
+  g_object_unref (dynamics->color_dynamics);
+  g_object_unref (dynamics->angle_dynamics);
+  g_object_unref (dynamics->jitter_dynamics);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
-static GimpDynamicsOutput*
-gimp_dynamics_output_init(void)
-{
-  GimpDynamicsOutput * dynamics = g_slice_new0 (GimpDynamicsOutput);
-
-  dynamics->pressure_curve = g_object_new (GIMP_TYPE_CURVE,
-                             "name",       "Pressure curve",
-                              NULL);
-  dynamics->velocity_curve = g_object_new (GIMP_TYPE_CURVE,
-                             "name",       "Velocity curve",
-                             NULL);
-  dynamics->direction_curve = g_object_new (GIMP_TYPE_CURVE,
-                              "name",       "Direction curve",
-                              NULL);
-  dynamics->tilt_curve      = g_object_new (GIMP_TYPE_CURVE,
-                              "name",       "Tilt curve",
-                              NULL);
-  dynamics->random_curve    = g_object_new (GIMP_TYPE_CURVE,
-                              "name",       "Random curve",
-                              NULL);
-  dynamics->fade_curve      = g_object_new (GIMP_TYPE_CURVE,
-                              "name",       "Fade curve",
-                              NULL);
-  return dynamics;
-}
-
-static void
-gimp_dynamics_output_finalize  (GimpDynamicsOutput *dynamics)
-{
-  g_object_unref(dynamics->pressure_curve);
-
-  g_object_unref(dynamics->velocity_curve);
-
-  g_object_unref(dynamics->direction_curve);
-
-  g_object_unref(dynamics->tilt_curve);
-
-  g_object_unref(dynamics->random_curve);
-
-  g_object_unref(dynamics->fade_curve);
-
-  g_slice_free (GimpDynamicsOutput,  dynamics);
-
-}
-
-
 static void
 gimp_dynamics_set_property (GObject      *object,
                             guint         property_id,
@@ -964,212 +897,8 @@ gimp_dynamics_get_extension (GimpData *data)
   return GIMP_DYNAMICS_FILE_EXTENSION;
 }
 
-gdouble
-gimp_dynamics_get_linear_output_val (GimpDynamicsOutput *output, GimpCoords coords, gdouble fade_point)
-{
-  gdouble total = 0.0;
-  gdouble factors = 0.0;
-
-  gdouble result = 1.0;
-
-  if (output->pressure)
-    {
-      total += coords.pressure;
-      factors++;
-    }
-
-  if (output->velocity)
-    {
-      total += (1.0 - coords.velocity);
-      factors++;
-    }
-
-  if (output->direction)
-    {
-      total += coords.direction + 0.5;
-      factors++;
-    }
-
-  if (output->tilt)
-    {
-      total += 1.0 - sqrt (SQR (coords.xtilt) + SQR (coords.ytilt));
-      factors++;
-    }
-
-  if (output->random)
-    {
-      total += g_random_double_range (0.0, 1.0);
-      factors++;
-    }
-
-  if (output->fade)
-    {
-      total += fade_point;
-      factors++;
-    }
-
-  if (factors > 0)
-    result = total / factors;
-
-  //printf("Dynamics queried(linear). Result: %f, factors: %f, total: %f \n", result, factors, total);
-  return result;
-}
-
-gdouble
-gimp_dynamics_get_angular_output_val (GimpDynamicsOutput *output, GimpCoords coords, gdouble fade_point)
-{
-  gdouble total = 0.0;
-  gdouble factors = 0.0;
-
-  gdouble result = 1.0;
-
-  if (output->pressure)
-    {
-      total += coords.pressure;
-      factors++;
-    }
-
-  if (output->velocity)
-    {
-      total += (1.0 - coords.velocity);
-      factors++;
-    }
-
-  if (output->direction)
-    {
-      total += coords.direction + 0.5;
-      factors++;
-    }
-/* For tilt to make sense, it needs to be converted to an angle, not just vector */
-  if (output->tilt)
-    {
-
-      gdouble tilt_x = coords.xtilt;
-      gdouble tilt_y = coords.ytilt;
-      gdouble tilt = 0.0;
-
-      if (tilt_x == 0.0)
-        {
-          if (tilt_y >= 0.0)
-            tilt = 0.5;
-          else if (tilt_y < 0.0)
-            tilt = 0.0;
-          else tilt = -1.0;
-        }
-      else
-        {
-          tilt = atan ((- 1.0 * tilt_y) /
-                                tilt_x) / (2 * G_PI);
-
-           if (tilt_x > 0.0)
-             tilt = tilt + 0.5;
-         }
-
-      tilt = tilt + 0.5; /* correct the angle, its wrong by 180 degrees */
-
-      while (tilt > 1.0)
-        tilt -= 1.0;
-
-      while (tilt < 0.0)
-        tilt += 1.0;
-      total += tilt;
-      factors++;
-    }
-
-  if (output->random)
-    {
-      total += g_random_double_range (0.0, 1.0);
-      factors++;
-    }
-
-  if (output->fade)
-    {
-      total += fade_point;
-      factors++;
-    }
-
-  if (factors > 0)
-    result = total / factors;
-
-   return result + 0.5;
-}
-
-
-gdouble
-gimp_dynamics_get_aspect_output_val (GimpDynamicsOutput *output, GimpCoords coords, gdouble fade_point)
-{
-  gdouble total = 0.0;
-  gdouble factors = 0.0;
-
-  gdouble result = 1.0;
-
-  if (output->pressure)
-    {
-      total += 2 * coords.pressure;
-      factors++;
-    }
-
-  if (output->velocity)
-    {
-      total += 2 * coords.velocity;
-      factors++;
-    }
-
-  if (output->direction)
-    {
-      gdouble direction = 0.0;
-      direction = fmod (1 + coords.direction, 0.5) / 0.25;
-
-      if ((coords.direction > 0.0) && (coords.direction < 0.5))
-        direction = 1 / direction;
-
-      total += direction;
-      factors++;
-    }
-
-  if (output->tilt)
-    {
-      total += sqrt ((1 - fabs (coords.xtilt)) / (1 - fabs (coords.ytilt)));
-      factors++;
-    }
-
-  if (output->random)
-    {
-      gdouble random = g_random_double_range (0.0, 1.0);
-      if (random <= 0.5)
-        {
-          random = 1 / (random / 0.5 * (2.0 - 1.0) + 1.0);
-        }
-      else
-        {
-          random = (random - 0.5) / (1.0 - 0.5) * (2.0 - 1.0) + 1.0;
-        }
-      total += random;
-      factors++;
-    }
-
-  if (output->fade)
-    {
-      total += fade_point;
-      factors++;
-    }
-
-  if (factors > 0)
-    result = total / factors;
-
-  /* printf("Dynamics queried(aspect). Result: %f, factors: %f, total: %f \n", result, factors, total);*/
-  return result;
-}
-
 gboolean
-gimp_dynamics_output_is_enabled(GimpDynamicsOutput *output)
-{
-  return (output->pressure || output->velocity || output->direction ||
-          output->tilt     || output->random   || output->fade);
-}
-
-gboolean
-gimp_dynamics_input_fade_enabled(GimpDynamics *dynamics)
+gimp_dynamics_input_fade_enabled (GimpDynamics *dynamics)
 {
   return (dynamics->opacity_dynamics->fade      ||
           dynamics->hardness_dynamics->fade     ||
@@ -1179,5 +908,4 @@ gimp_dynamics_input_fade_enabled(GimpDynamics *dynamics)
           dynamics->color_dynamics->fade        ||
           dynamics->jitter_dynamics->fade       ||
           dynamics->angle_dynamics->fade);
-
 }
diff --git a/app/core/gimpdynamics.h b/app/core/gimpdynamics.h
index 58deff4..b96167c 100644
--- a/app/core/gimpdynamics.h
+++ b/app/core/gimpdynamics.h
@@ -22,24 +22,6 @@
 #include "gimpdata.h"
 
 
-struct _GimpDynamicsOutput
-{
-  gboolean   pressure;
-  gboolean   velocity;
-  gboolean   direction;
-  gboolean   tilt;
-  gboolean   random;
-  gboolean   fade;
-
-  GimpCurve *pressure_curve;
-  GimpCurve *velocity_curve;
-  GimpCurve *direction_curve;
-  GimpCurve *tilt_curve;
-  GimpCurve *random_curve;
-  GimpCurve *fade_curve;
-};
-
-
 #define GIMP_TYPE_DYNAMICS            (gimp_dynamics_get_type ())
 #define GIMP_DYNAMICS(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_DYNAMICS, GimpDynamics))
 #define GIMP_DYNAMICS_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_DYNAMICS, GimpDynamicsClass))
@@ -70,27 +52,12 @@ struct _GimpDynamicsClass
 };
 
 
-GType      gimp_dynamics_get_type               (void) G_GNUC_CONST;
-
-GimpData * gimp_dynamics_new                    (const gchar        *name);
-
-GimpData * gimp_dynamics_get_standard           (void);
-
-gdouble    gimp_dynamics_get_linear_output_val  (GimpDynamicsOutput *output,
-                                                 GimpCoords          coords,
-                                                 gdouble             fade_point);
-
-gdouble    gimp_dynamics_get_angular_output_val (GimpDynamicsOutput *output,
-                                                 GimpCoords          coords,
-                                                 gdouble             fade_point);
-
-gdouble    gimp_dynamics_get_aspect_output_val  (GimpDynamicsOutput *output,
-                                                 GimpCoords          coords,
-                                                 gdouble             fade_point);
+GType      gimp_dynamics_get_type           (void) G_GNUC_CONST;
 
-gboolean   gimp_dynamics_output_is_enabled      (GimpDynamicsOutput *output);
+GimpData * gimp_dynamics_new                (const gchar  *name);
+GimpData * gimp_dynamics_get_standard       (void);
 
-gboolean   gimp_dynamics_input_fade_enabled     (GimpDynamics       *dynamics);
+gboolean   gimp_dynamics_input_fade_enabled (GimpDynamics *dynamics);
 
 
 #endif  /*  __GIMP_DYNAMICS_H__  */
diff --git a/app/core/gimpdynamicsoutput.c b/app/core/gimpdynamicsoutput.c
new file mode 100644
index 0000000..7b605c4
--- /dev/null
+++ b/app/core/gimpdynamicsoutput.c
@@ -0,0 +1,438 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+
+#include "libgimpmath/gimpmath.h"
+#include "libgimpconfig/gimpconfig.h"
+
+#include "core-types.h"
+
+#include "gimpcurve.h"
+#include "gimpdynamicsoutput.h"
+
+#include "gimp-intl.h"
+
+
+#define DEFAULT_PRESSURE  FALSE
+#define DEFAULT_VELOCITY  FALSE
+#define DEFAULT_DIRECTION FALSE
+#define DEFAULT_TILT      FALSE
+#define DEFAULT_RANDOM    FALSE
+#define DEFAULT_FADE      FALSE
+
+
+enum
+{
+  PROP_0,
+
+  PROP_PRESSURE,
+  PROP_VELOCITY,
+  PROP_DIRECTION,
+  PROP_TILT,
+  PROP_RANDOM,
+  PROP_FADE
+};
+
+
+static void   gimp_dynamics_output_finalize     (GObject      *object);
+
+static void   gimp_dynamics_output_set_property (GObject      *object,
+                                                 guint         property_id,
+                                                 const GValue *value,
+                                                 GParamSpec   *pspec);
+
+static void   gimp_dynamics_output_get_property (GObject      *object,
+                                                 guint         property_id,
+                                                 GValue       *value,
+                                                 GParamSpec   *pspec);
+
+
+G_DEFINE_TYPE (GimpDynamicsOutput, gimp_dynamics_output,
+               GIMP_TYPE_OBJECT)
+
+
+#define parent_class gimp_dynamics_output_parent_class
+
+
+static void
+gimp_dynamics_output_class_init (GimpDynamicsOutputClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize     = gimp_dynamics_output_finalize;
+  object_class->set_property = gimp_dynamics_output_set_property;
+  object_class->get_property = gimp_dynamics_output_get_property;
+
+  GIMP_CONFIG_INSTALL_PROP_BOOLEAN (object_class, PROP_PRESSURE,
+                                    "pressure", NULL,
+                                    DEFAULT_PRESSURE,
+                                    GIMP_PARAM_STATIC_STRINGS);
+
+  GIMP_CONFIG_INSTALL_PROP_BOOLEAN (object_class, PROP_VELOCITY,
+                                    "velocity", NULL,
+                                    DEFAULT_VELOCITY,
+                                    GIMP_PARAM_STATIC_STRINGS);
+
+  GIMP_CONFIG_INSTALL_PROP_BOOLEAN (object_class, PROP_DIRECTION,
+                                    "direction", NULL,
+                                    DEFAULT_DIRECTION,
+                                    GIMP_PARAM_STATIC_STRINGS);
+
+  GIMP_CONFIG_INSTALL_PROP_BOOLEAN (object_class, PROP_TILT,
+                                    "tilt", NULL,
+                                    DEFAULT_TILT,
+                                    GIMP_PARAM_STATIC_STRINGS);
+
+  GIMP_CONFIG_INSTALL_PROP_BOOLEAN (object_class, PROP_RANDOM,
+                                    "random", NULL,
+                                    DEFAULT_RANDOM,
+                                    GIMP_PARAM_STATIC_STRINGS);
+
+  GIMP_CONFIG_INSTALL_PROP_BOOLEAN (object_class, PROP_FADE,
+                                    "fade", NULL,
+                                    DEFAULT_FADE,
+                                    GIMP_PARAM_STATIC_STRINGS);
+}
+
+static void
+gimp_dynamics_output_init (GimpDynamicsOutput *output)
+{
+  output->pressure_curve  = GIMP_CURVE (gimp_curve_new ("Pressure curve"));
+  output->velocity_curve  = GIMP_CURVE (gimp_curve_new ("Velocity curve"));
+  output->direction_curve = GIMP_CURVE (gimp_curve_new ("Direction curve"));
+  output->tilt_curve      = GIMP_CURVE (gimp_curve_new ("Tilt curve"));
+  output->random_curve    = GIMP_CURVE (gimp_curve_new ("Random curve"));
+  output->fade_curve      = GIMP_CURVE (gimp_curve_new ("Fade curve"));
+}
+
+static void
+gimp_dynamics_output_finalize (GObject *object)
+{
+  GimpDynamicsOutput *output = GIMP_DYNAMICS_OUTPUT (object);
+
+  g_object_unref (output->pressure_curve);
+  g_object_unref (output->velocity_curve);
+  g_object_unref (output->direction_curve);
+  g_object_unref (output->tilt_curve);
+  g_object_unref (output->random_curve);
+  g_object_unref (output->fade_curve);
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gimp_dynamics_output_set_property (GObject      *object,
+                                   guint         property_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+  GimpDynamicsOutput *output = GIMP_DYNAMICS_OUTPUT (object);
+
+  switch (property_id)
+    {
+    case PROP_PRESSURE:
+      output->pressure = g_value_get_boolean (value);
+      break;
+
+    case PROP_VELOCITY:
+      output->velocity = g_value_get_boolean (value);
+      break;
+
+    case PROP_DIRECTION:
+      output->direction = g_value_get_boolean (value);
+      break;
+
+    case PROP_TILT:
+      output->tilt = g_value_get_boolean (value);
+      break;
+
+    case PROP_RANDOM:
+      output->random = g_value_get_boolean (value);
+      break;
+
+    case PROP_FADE:
+      output->fade = g_value_get_boolean (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_dynamics_output_get_property (GObject    *object,
+                                   guint       property_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+  GimpDynamicsOutput *output = GIMP_DYNAMICS_OUTPUT (object);
+
+  switch (property_id)
+    {
+    case PROP_PRESSURE:
+      g_value_set_boolean (value, output->pressure);
+      break;
+
+    case PROP_VELOCITY:
+      g_value_set_boolean (value, output->velocity);
+      break;
+
+    case PROP_DIRECTION:
+      g_value_set_boolean (value, output->direction);
+      break;
+
+    case PROP_TILT:
+      g_value_set_boolean (value, output->tilt);
+      break;
+
+    case PROP_RANDOM:
+      g_value_set_boolean (value, output->random);
+      break;
+
+    case PROP_FADE:
+      g_value_set_boolean (value, output->fade);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+GimpDynamicsOutput *
+gimp_dynamics_output_new (const gchar *name)
+{
+  g_return_val_if_fail (name != NULL, NULL);
+
+  return g_object_new (GIMP_TYPE_DYNAMICS_OUTPUT,
+                       "name", name,
+                       NULL);
+}
+
+gdouble
+gimp_dynamics_get_linear_output_val (GimpDynamicsOutput *output,
+                                     GimpCoords          coords,
+                                     gdouble             fade_point)
+{
+  gdouble total = 0.0;
+  gdouble factors = 0.0;
+
+  gdouble result = 1.0;
+
+  if (output->pressure)
+    {
+      total += coords.pressure;
+      factors++;
+    }
+
+  if (output->velocity)
+    {
+      total += (1.0 - coords.velocity);
+      factors++;
+    }
+
+  if (output->direction)
+    {
+      total += coords.direction + 0.5;
+      factors++;
+    }
+
+  if (output->tilt)
+    {
+      total += 1.0 - sqrt (SQR (coords.xtilt) + SQR (coords.ytilt));
+      factors++;
+    }
+
+  if (output->random)
+    {
+      total += g_random_double_range (0.0, 1.0);
+      factors++;
+    }
+
+  if (output->fade)
+    {
+      total += fade_point;
+      factors++;
+    }
+
+  if (factors > 0)
+    result = total / factors;
+
+  //printf("Dynamics queried(linear). Result: %f, factors: %f, total: %f \n", result, factors, total);
+  return result;
+}
+
+gdouble
+gimp_dynamics_get_angular_output_val (GimpDynamicsOutput *output,
+                                      GimpCoords          coords,
+                                      gdouble             fade_point)
+{
+  gdouble total = 0.0;
+  gdouble factors = 0.0;
+
+  gdouble result = 1.0;
+
+  if (output->pressure)
+    {
+      total += coords.pressure;
+      factors++;
+    }
+
+  if (output->velocity)
+    {
+      total += (1.0 - coords.velocity);
+      factors++;
+    }
+
+  if (output->direction)
+    {
+      total += coords.direction + 0.5;
+      factors++;
+    }
+/* For tilt to make sense, it needs to be converted to an angle, not just vector */
+  if (output->tilt)
+    {
+
+      gdouble tilt_x = coords.xtilt;
+      gdouble tilt_y = coords.ytilt;
+      gdouble tilt = 0.0;
+
+      if (tilt_x == 0.0)
+        {
+          if (tilt_y >= 0.0)
+            tilt = 0.5;
+          else if (tilt_y < 0.0)
+            tilt = 0.0;
+          else tilt = -1.0;
+        }
+      else
+        {
+          tilt = atan ((- 1.0 * tilt_y) /
+                                tilt_x) / (2 * G_PI);
+
+           if (tilt_x > 0.0)
+             tilt = tilt + 0.5;
+         }
+
+      tilt = tilt + 0.5; /* correct the angle, its wrong by 180 degrees */
+
+      while (tilt > 1.0)
+        tilt -= 1.0;
+
+      while (tilt < 0.0)
+        tilt += 1.0;
+      total += tilt;
+      factors++;
+    }
+
+  if (output->random)
+    {
+      total += g_random_double_range (0.0, 1.0);
+      factors++;
+    }
+
+  if (output->fade)
+    {
+      total += fade_point;
+      factors++;
+    }
+
+  if (factors > 0)
+    result = total / factors;
+
+   return result + 0.5;
+}
+
+
+gdouble
+gimp_dynamics_get_aspect_output_val (GimpDynamicsOutput *output,
+                                     GimpCoords          coords,
+                                     gdouble             fade_point)
+{
+  gdouble total = 0.0;
+  gdouble factors = 0.0;
+
+  gdouble result = 1.0;
+
+  if (output->pressure)
+    {
+      total += 2 * coords.pressure;
+      factors++;
+    }
+
+  if (output->velocity)
+    {
+      total += 2 * coords.velocity;
+      factors++;
+    }
+
+  if (output->direction)
+    {
+      gdouble direction = 0.0;
+      direction = fmod (1 + coords.direction, 0.5) / 0.25;
+
+      if ((coords.direction > 0.0) && (coords.direction < 0.5))
+        direction = 1 / direction;
+
+      total += direction;
+      factors++;
+    }
+
+  if (output->tilt)
+    {
+      total += sqrt ((1 - fabs (coords.xtilt)) / (1 - fabs (coords.ytilt)));
+      factors++;
+    }
+
+  if (output->random)
+    {
+      gdouble random = g_random_double_range (0.0, 1.0);
+      if (random <= 0.5)
+        {
+          random = 1 / (random / 0.5 * (2.0 - 1.0) + 1.0);
+        }
+      else
+        {
+          random = (random - 0.5) / (1.0 - 0.5) * (2.0 - 1.0) + 1.0;
+        }
+      total += random;
+      factors++;
+    }
+
+  if (output->fade)
+    {
+      total += fade_point;
+      factors++;
+    }
+
+  if (factors > 0)
+    result = total / factors;
+
+  /* printf("Dynamics queried(aspect). Result: %f, factors: %f, total: %f \n", result, factors, total);*/
+  return result;
+}
+
+gboolean
+gimp_dynamics_output_is_enabled (GimpDynamicsOutput *output)
+{
+  return (output->pressure || output->velocity || output->direction ||
+          output->tilt     || output->random   || output->fade);
+}
diff --git a/app/core/gimpdynamicsoutput.h b/app/core/gimpdynamicsoutput.h
new file mode 100644
index 0000000..e27147b
--- /dev/null
+++ b/app/core/gimpdynamicsoutput.h
@@ -0,0 +1,79 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995-1999 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_DYNAMICS_OUTPUT_H__
+#define __GIMP_DYNAMICS_OUTPUT_H__
+
+
+#include "gimpobject.h"
+
+
+#define GIMP_TYPE_DYNAMICS_OUTPUT            (gimp_dynamics_output_get_type ())
+#define GIMP_DYNAMICS_OUTPUT(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_DYNAMICS_OUTPUT, GimpDynamicsOutput))
+#define GIMP_DYNAMICS_OUTPUT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_DYNAMICS_OUTPUT, GimpDynamicsOutputClass))
+#define GIMP_IS_DYNAMICS_OUTPUT(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_DYNAMICS_OUTPUT))
+#define GIMP_IS_DYNAMICS_OUTPUT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_DYNAMICS_OUTPUT))
+#define GIMP_DYNAMICS_OUTPUT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_DYNAMICS_OUTPUT, GimpDynamicsOutputClass))
+
+
+typedef struct _GimpDynamicsOutputClass GimpDynamicsOutputClass;
+
+struct _GimpDynamicsOutput
+{
+  GimpObject parent_instance;
+
+  gboolean   pressure;
+  gboolean   velocity;
+  gboolean   direction;
+  gboolean   tilt;
+  gboolean   random;
+  gboolean   fade;
+
+  GimpCurve *pressure_curve;
+  GimpCurve *velocity_curve;
+  GimpCurve *direction_curve;
+  GimpCurve *tilt_curve;
+  GimpCurve *random_curve;
+  GimpCurve *fade_curve;
+};
+
+struct _GimpDynamicsOutputClass
+{
+  GimpObjectClass  parent_class;
+};
+
+
+GType                gimp_dynamics_output_get_type (void) G_GNUC_CONST;
+
+GimpDynamicsOutput * gimp_dynamics_output_new      (const gchar *name);
+
+gdouble    gimp_dynamics_get_linear_output_val  (GimpDynamicsOutput *output,
+                                                 GimpCoords          coords,
+                                                 gdouble             fade_point);
+
+gdouble    gimp_dynamics_get_angular_output_val (GimpDynamicsOutput *output,
+                                                 GimpCoords          coords,
+                                                 gdouble             fade_point);
+
+gdouble    gimp_dynamics_get_aspect_output_val  (GimpDynamicsOutput *output,
+                                                 GimpCoords          coords,
+                                                 gdouble             fade_point);
+
+gboolean   gimp_dynamics_output_is_enabled      (GimpDynamicsOutput *output);
+
+
+#endif  /*  __GIMP_DYNAMICS_OUTPUT_H__  */
diff --git a/app/paint/gimpbrushcore.h b/app/paint/gimpbrushcore.h
index c82b450..b2bfc74 100644
--- a/app/paint/gimpbrushcore.h
+++ b/app/paint/gimpbrushcore.h
@@ -20,7 +20,10 @@
 
 
 #include "gimppaintcore.h"
+
+#warning FIXME: remove these asap!
 #include "core/gimpdynamics.h"
+#include "core/gimpdynamicsoutput.h"
 
 #define BRUSH_CORE_SUBSAMPLE        4
 #define BRUSH_CORE_SOLID_SUBSAMPLE  2



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]