Understanding the Hamiltonian function

Based on this function:

$$\text{max} \int_0^2(-2tx-u^2) \, dt$$

We know that $$(1) \;-1 \leq u \leq 1, \; \; \; (2) \; \dot{x}=2u, \; \; \; (3) \; x(0)=1, \; \; \; \text{x(2) is free}$$

I can rewrite the function into a hamiltonian function:

$$H=-2tx-u^2+p2u$$

where u(t) maxizmizee H where:

$$u = \left\{\begin{array}{rc} 1 & p \geq 1 \\ p & -1 < p < 1 \\ -1 & p \leq -1 \end{array}\right.$$

Now, can somebody help me understand how the last part is true, and why? I find it hard to see the bridge between $u$ and $p$.

I'd like to add some detail to the other answer posted here. First note that $$H = -2tx - u^2 + 2pu$$ and that we'd like to maximize $H$. There are two terms containing $u$ and hence Pontryagin's Maximum Principle tells us that at each time $t \in [0,2]$, we should choose $u$ so that it maximizes $$-u^2 + 2pu = u(-u + 2p).$$ This function is concave in $u$ and we can take its derivative to find stationary points (this is point-wise in time and stationarity here is not meant in the same sense as it was used above) and concavity guarantees that any stationary point is a maximum. Doing so, we have $$\frac{\partial }{\partial u} \big(-u^2 + 2pu\big) = -2u + 2p = 0.$$ From this we see that we'd like $u = p$. The constraints on $u$ prevent us from doing this when $p \not\in [-1,1]$. In those cases, we set $u$ to be as close to $p$ as possible, which gives $$u = \left\{\begin{array}{rc} 1 & p \geq 1 \\ p & -1 < p < 1 \\ -1 & p \leq -1, \end{array}\right.,$$ just as you have above.
$$\frac{\partial H}{\partial u} = -2u + 2p \tag{1}$$ where $u$ is the control variable and $p$ is the costate.
The optimality of $H$ requires (1)=0, where you obtain your $u_t$ expression considering its constraint.