Basic Solid support and continuing form abuse

78b7a65 · gmorell · 2022-12-18 07:21

21 files +153 -13

Files changed

modified lib/lambent_ex/machine/steps/chase.ex
+1 −1
@@ -1,6 +1,6 @@
1 1 defmodule LambentEx.Machine.Steps.Chase.Cfg do
2 2 # @enforce_keys [:h]
3 − defstruct [:h, s: 255, v: 0, spacing: 30, fadeby: 15, id: 0, status: 0, window: 0]
3 + defstruct [:h, s: 255, v: 255, spacing: 30, fadeby: 18, id: 0, status: 0, window: 0]
4 4 end
5 5
6 6 defmodule LambentEx.Machine.Steps.Chase do
added lib/lambent_ex/machine/steps/solid.ex
+46 −0
@@ -0,0 +1,46 @@
1 +defmodule LambentEx.Machine.Steps.Solid.Cfg do
2 + # @enforce_keys [:h]
3 + defstruct [:h, s: 255, v: 255, id: 0]
4 +end
5 +
6 +defmodule LambentEx.Machine.Steps.Solid do
7 + @moduledoc false
8 + use GenServer
9 +
10 + @registry :lambent_steps
11 + @cls :solid
12 +
13 + alias LambentEx.Machine.Steps.Solid.Cfg
14 + alias LambentEx.Utils.Color
15 +
16 + defp n(opts) do
17 + "#{@cls}-#{opts[:name]}-#{opts[:id]}"
18 + end
19 +
20 + def start_link(opts) do
21 + GenServer.start_link(__MODULE__, opts, name: via_tuple(n(opts)))
22 + end
23 +
24 + def init(opts) do
25 + opts = opts |> Map.put(:status, opts.id)
26 + cfg = struct(%LambentEx.Machine.Steps.Solid.Cfg{}, opts)
27 + {:ok, cfg}
28 + end
29 +
30 + defp via_tuple(name) do
31 + {:via, Registry, {@registry, name}}
32 + end
33 +
34 + defp read(state) do
35 + Color.hsv2rgb([state.h, state.s, state.v])
36 + end
37 +
38 + def handle_cast(:step, state) do
39 + {:noreply, state}
40 + end
41 +
42 + def handle_call(:read, _from, state) do
43 + {:reply, read(state), state}
44 + end
45 +
46 +end
modified lib/lambent_ex/schema/machines.ex
+4 −2
@@ -34,8 +34,10 @@ defmodule LambentEx.Schema.Machines do
34 34
35 35 field(:class, PolymorphicEmbed,
36 36 types: [
37 − static: RoomSanctum.Configuration.Queries.AQI,
38 − chase: RoomSanctum.Configuration.Queries.Calendar
37 +# gen_solid: LambentEx.Schema.Steps.Solid,
38 +# gen_solid_h: LambentEx.Schema.Steps.Solid,
39 + gen_chaser: LambentEx.Schema.Steps.Chase,
40 + gen_chaser_h: LambentEx.Schema.Steps.Chase,
39 41 ],
40 42 on_type_not_found: :raise,
41 43 on_replace: :update
added lib/lambent_ex/schema/steps/chase.ex
+20 −0
@@ -0,0 +1,20 @@
1 +defmodule LambentEx.Schema.Steps.Chase do
2 + use Ecto.Schema
3 + import Ecto.Changeset
4 +
5 + @primary_key false
6 +
7 + embedded_schema do
8 + field :h, :integer
9 + field :s, :integer, default: 255
10 + field :v, :integer, default: 255
11 + field :spacing, :integer, default: 30
12 + field :fadeby, :integer, default: 15
13 + end
14 +
15 + def changeset(source, params) do
16 + source
17 + |> cast(params, ~w(h s v spacing fadeby)a)
18 + |> validate_required([:h, :s, :v, :spacing, :fadeby])
19 + end
20 +end
modified lib/lambent_ex_web.ex
+3 −0
@@ -94,6 +94,9 @@ defmodule LambentExWeb do
94 94 # Custom Components
95 95 import LambentEx.Components.Buttons
96 96
97 + # Polymorphic Embed
98 + import PolymorphicEmbed.HTML.Form
99 +
97 100 # Routes generation with the ~p sigil
98 101 unquote(verified_routes())
99 102 end
modified lib/lambent_ex_web/components/core_components.ex
+2 −2
@@ -58,7 +58,7 @@ defmodule LambentExWeb.CoreComponents do
58 58 tabindex="0"
59 59 >
60 60 <div class="flex min-h-full items-center justify-center">
61 − <div class="w-full max-w-3xl p-4 sm:p-6 lg:py-8">
61 + <div class="w-full max-w-5xl p-4 sm:p-6 lg:py-8">
62 62 <.focus_wrap
63 63 id={"#{@id}-container"}
64 64 phx-mounted={@show && show_modal(@id)}
@@ -352,7 +352,7 @@ defmodule LambentExWeb.CoreComponents do
352 352 value={@value}
353 353 class={[
354 354 input_border(@errors),
355 − "input input-bordered"
355 + "input input-bordered w-full"
356 356 ]}
357 357 {@rest}
358 358 />
modified lib/lambent_ex_web/live/links/form_component.ex
+1 −1
@@ -29,7 +29,7 @@ defmodule LambentEx.LinksLive.FormComponent do
29 29 {:ok, _link} ->
30 30 {:noreply,
31 31 socket
32 − |> put_flash(:info, "Testyyyy updated successfully")
32 + |> put_flash(:info, "Link Created Successfully")
33 33 |> push_navigate(to: socket.assigns.navigate)}
34 34
35 35 {:error, %Ecto.Changeset{} = changeset} ->
modified lib/lambent_ex_web/live/links/form_component.html.heex
+1 −1
@@ -9,7 +9,7 @@
9 9 phx-submit="save"
10 10 >
11 11
12 − <.input field={{f, :name}} type="text" label="Name" options={1..2}>
12 + <.input field={{f, :name}} type="text" label="Name" >
13 13 <:icon_pre>
14 14 <btn class="btn btn-accent" phx-click="new-name" phx-target={@myself}>
15 15 <i class="fa-solid fa-arrows-rotate"></i>
modified lib/lambent_ex_web/live/machines/form_component.ex
+33 −2
@@ -11,7 +11,15 @@ defmodule LambentEx.MachinesLive.FormComponent do
11 11 {:ok,
12 12 socket
13 13 |> assign(assigns)
14 − |> assign(:changeset, changeset)}
14 + |> assign(:changeset, changeset)
15 + |> assign(:rgb, nil)
16 + |> assign(:h, nil)
17 + |> assign(:s, nil)
18 + |> assign(:v, nil)
19 + |> assign(:r, nil)
20 + |> assign(:g, nil)
21 + |> assign(:b, nil)
22 + }
15 23 end
16 24
17 25 @impl true
@@ -20,10 +28,33 @@ defmodule LambentEx.MachinesLive.FormComponent do
20 28 socket.assigns.machine
21 29 |> Machines.change_machine(machine_params)
22 30 |> Map.put(:action, :validate)
23 − |> IO.inspect()
31 +
32 + IO.inspect(changeset)
33 + keys = case changeset.changes[:type] do
34 + :gen_chaser_h -> changeset.changes[:class] |> hue_keys
35 + _otherwise -> []
36 + end
37 +
38 + socket = case keys do
39 + [r: r, g: g, b: b] -> socket |> assign(:rgb, "rgb(#{r}, #{g}, #{b})")
40 + [] -> socket
41 + end
24 42
25 43 {:noreply, assign(socket, :changeset, changeset)}
26 44 end
27 45
46 + def handle_event("new-name", _params, socket) do
47 + changeset = socket.assigns.changeset |> Ecto.Changeset.change(name: Names.name())
48 + {:noreply, assign(socket, :changeset, changeset)}
49 + end
50 +
28 51 defp values, do: LambentEx.Schema.Machines.values |> Enum.map(fn {k,v} -> {v,k} end) |> Map.new
52 + defp expand(int) do
53 + (int / 255 * 360) |> round
54 + end
55 + defp hue_keys(nil), do: []
56 + defp hue_keys(%{h: h, s: s, v: v}) do
57 + [r,g,b] = LambentEx.Utils.Color.hsv2rgb([h,s,v])
58 + [r: r, g: g, b: b]
59 + end
29 60 end
modified lib/lambent_ex_web/live/machines/form_component.html.heex
+42 −4
@@ -9,11 +9,49 @@
9 9 phx-submit="save"
10 10 >
11 11 <div class="flex">
12 − <div class="flex-none">
13 − <.input field={{f, :type}} type="select" label="Class" options={values} />
12 + <div class="flex-none p-2">
13 + <.input field={{f, :name}} type="text" label="Name">
14 + <:icon_pre>
15 + <btn class="btn btn-accent" phx-click="new-name" phx-target={@myself}>
16 + <i class="fa-solid fa-arrows-rotate"></i>
17 + </btn>
18 + </:icon_pre>
19 + </.input>
20 + <.input field={{f, :type}} type="select" label="Machine Type" options={values} prompt="Select" />
14 21 </div>
15 − <div class="flex-auto">
16 − The Form For <%= @changeset.changes |> Map.get(:type)%>
22 + <div class="flex-auto p-2">
23 + <%= case @changeset.changes |> Map.get(:type) do %>
24 + <%= nil -> %>
25 + <h2 class="text-4xl text-bold text-accent">
26 + Select a machine type on the left to configure them.
27 + </h2>
28 + <%= :gen_chaser -> %> rgb
29 + <%= :gen_chaser_h -> %>
30 + <%= polymorphic_embed_inputs_for f, :class, :gen_chaser_h, fn gch -> %>
31 + <.input field={{gch, :h}} type="range" min="0" max="255" label="Hue" class="range range-priamry range-lg" />
32 + <div class="w-full flex justify-between text-xs px-2 mb-4" style={"color: #{@rgb}"}>
33 + <span>&#9608;</span>
34 + <span>&#9608;</span>
35 + <span>&#9608;</span>
36 + <span>&#9608;</span>
37 + <span>&#9608;</span>
38 + <span>&#9608;</span>
39 + <span>&#9608;</span>
40 + </div>
41 + <div tabindex="0" class="collapse collapse-arrow border border-base-300 bg-base-100 rounded-box">
42 + <div class="collapse-title text-xl font-medium">
43 + Extra Configuration Values
44 + </div>
45 + <div class="collapse-content">
46 + <p>
47 + <.input field={{gch, :s}} type="range" min="0" max="255" label="Saturation" class="range range-priamry range-lg" />
48 + <.input field={{gch, :v}} type="range" min="0" max="255" label="Saturation" class="range range-priamry range-lg" />
49 + </p>
50 + </div>
51 + </div>
52 + <% end %>
53 +
54 + <% end %>
17 55 </div>
18 56 </div>
19 57

Parents: 052b2dd